views:

207

answers:

2

I am building a blog-post detail page on my site that will display display a 'previous' and 'next' link similar to how a typepad blog post works. See example below. alt text

I am wondering if there is a way to query the database which accomplishes something like the image below where it selects the 'current' record (in yellow) for display, but also selects the next and previous records (in green) when sorted by PublishDate.

alt text

Also, for clarification, the database table I am working with has unique publish dates. Sorry that the example image shows multiple records with the same PublishDate.

+10  A: 

You dont. SQL has no previous, next concept. YOu basically ask for the next top 1 in an ordered query (by date for example) after the one you already have. THis means you need to have / impost an order (by order by statement) for filtering.

TomTom
+1 Good solution! =)
Will Marcouiller
In the question I stated that I was already sorting the records by a unique PublishDate. I want to know if it is possible to make 1 round-trip to SQL server to select a record by ID as well as the records 'before' and 'after' it when sorted.
jessegavin
The answer is no then.
TomTom
Thanks @TomTom.
jessegavin
+4  A: 

You need a means of ordering the posts. If you have that, then you could do something like the following in SQL Server 2005+ where you pass in the item number you want:

With OrderedPosts As
    (
    Select ...
        , ROW_NUMBER() OVER ( ORDER BY PublishDate ) As ItemRank
    From ..
    Where ...
    )
Select
From OrderedPosts
Where ItemRank = @ItemNumber

In the site code, you would need to keep track of what number you were currently on and then subtract one and requery to get the previous or add one and requery to get next.

If you wanted the next and previous along with the current in a single query, then you could do something like:

With OrderedPosts As
    (
    Select ...
        , ROW_NUMBER() OVER ( ORDER BY PublishDate ) As ItemRank
    From ..
    Where ...
    )
Select
From OrderedPosts
Where ItemRank Between (@ItemNumber - 1) And (@ItemNumber + 1)
Thomas
I think this example works well for scenarios in which I already have the notion of an 'index' in place (e.g. position 5 of 100). However in my situation, the identifier I want to pass into the query is a guid. - For example if the page url was `abc.com/post.aspx?id={guid}` and then let's say a user got to that page from a Google result, then how would I find the @ItemNumber?
jessegavin
@jessegavin - You will *must* impose a sequence/order to have to a notion of previous and next. If you allow a user to hit a page based on some id, you must have a means to determine where in the sequence that item resides and to do that you must impose that sequence yourself using something like ROW_NUMBER(). Google does not allow for this. There is no means from a single link to determine that a given link will appear on page X if the user searches for "Foo". You have to determine that manually as Google simply uses a count of results and a starting number to display its results.
Thomas
Just to clarify, I wasn't suggesting that I would rely on Google to manage a record index or anything. I mentioned Google as a possible referrer from which a user might end up at a particular page in my site. - I guess my problem comes in that I don't want to have to first run a query to determine the @ItemNumber for a given guid so that I can run the second query using the syntax you provided above. - Thank you for your answer though. +1
jessegavin