Maggie Nelson

Peek into the past and the future with LAG and LEAD

by maggie on Sep.21, 2007, under entry

Traditionally (or at least wherever I have seen it), doing queries that involve getting one row of data that combines data from many rows is kind of complicated. Probably as complicated as it sounds. The approaches usually end up in cartesian joins, complicated subqueries, and plenty of joins on the same table.
An example situation is when you want to get a specific blog post for your blog along with some minimal information about a previous and a next post. In this case, you want just one row, with the data about the current blog post and just another field or two for the titles of the previous and next post. Here’s how Oracle’s LAG and LEAD functions can help you out.


Let’s say your database table for blogs is something like this:

BLOG_ENTRY
-----------------
id (PK)
title
body
created_date

Here’s the SQL that returns the id and title of the current blog post, along with the id of the previous and the next one:

SELECT *
FROM (SELECT id,
LEAD(id, 1) OVER (ORDER BY created_date DESC) AS prev_id,
LAG(id, 1) OVER (ORDER BY created_date DESC) AS next_id
FROM blog_entry)
WHERE id = :id;

Ta-da!
Two things worthy of notice:

  • 1. The 2nd parameter to LEAD and LAG is the offset from the current row. It is not calculating the offset based on the id (which just happens to be numeric), but the number of the row – very similar to the ROWNUM function.
  • 2. LEAD and LAG can take an optional 3rd parameter, which will specify a default value to return. If you don’t specify a default, these functions will return NULL. If you wish, you can leverage this in your application to not show a “Previous” or “Next” link if that value is NULL.
2 comments for this entry:
  1. Ben Ramsey

    So… I guess this means I’ll get those previous and next links after all? ;-)

  2. Maggie Nelson

    Yes, but only because there is a cool Oracle function to do it. :)

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Tags

RSS Planet PHP

  • An intriguing use of lambda functions
    I’ve been working hard on Goodsie.com lately trying to bring it to launch. It’s been great being in on a new PHP project from (near) the beginning, as it frees up a number of things. One of those, is the fact that I can be using PHP 5.3 and all the new features that come with PHP 5.3. While I’ve used my fair share of the short-cut ternary already (?:), the […]
    Eli White
  • Accelerando
    This is a very unusual blog post for the open source area of my website, since it contains a recommendation for a science fiction book. The reason I've put it here instead of the private section is on the one hand, that it will definitely reach more geek - and therefor probably scifi interested - people here. On the other hand, the book I'm writing […]
    Tobias Schlitt
  • First year of Qaiku, and a travel writing challenge
    Qaiku, the conversational microblogging service that launched a year ago had a refresh that launched today. While it hasn't yet convinced the twittering masses, it has already proven itself as a lot more thoughtful platform for the Finnish online community, and as a valuable workstreaming tool. The new version looks quite nice and fresh. Notice the priv […]
    Henri Bergius
  • Getting started with the Midgard content repository
    I'm doing a talk today in the Bossa Conference about using Midgard as a content repository for mobile applications. As part of my presentation I wrote some simple example code for using the Midgard APIs in Python, and thought they would be good to share to those not attending the event as well. The idea of a content repository is that instead of coming […]
    Henri Bergius
  • Neural Networks in PHP
    Neural Networks in PHP By Louis Stowasser Neural networks allow emulating the behavior of a brain in software applications. Neural Networks have always had a too steep learning curve to venture towards, especially in a Web environment. Neural Mesh is an open source, pure PHP code based Neural Network manager and framework that makes it easier to work with […]
    PHP Classes