<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How to find next and previous records in SQL</title>
	<atom:link href="http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/</link>
	<description>Stay curious!</description>
	<lastBuildDate>Thu, 09 Feb 2012 09:56:43 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Feroz</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-17827</link>
		<dc:creator>Feroz</dc:creator>
		<pubDate>Tue, 16 Feb 2010 23:53:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=141#comment-17827</guid>
		<description>SELECT id FROM table_name WHERE primarykey  5 ORDER BY ID LIMIT 1</description>
		<content:encoded><![CDATA[<p>SELECT id FROM table_name WHERE primarykey  5 ORDER BY ID LIMIT 1</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tSQL</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-16751</link>
		<dc:creator>tSQL</dc:creator>
		<pubDate>Wed, 12 Aug 2009 13:25:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=141#comment-16751</guid>
		<description>CREATE PROCEDURE dbo.[GetPrevNext]
	@CityID INT,
	@LocationID INT
AS
DECLARE
	@Prev INT,
	@Next INT,
	@Row  INT,
	@Rows INT
BEGIN
	SET NOCOUNT ON
	DECLARE @locations TABLE (Row INT IDENTITY(1, 1), LocationID INT)

	INSERT @locations
	select
		-- to add and order by enable this
		-- and disable identit on row column in temp table
		--Row = ROW_NUMBER() OVER (ORDER BY A.LocationID ASC),
		LocationID = A.LocationID
	from
		Gmap_Locations AS A
	where
		A.CityID = @CityID

	SET NOCOUNT OFF
	
	SET @Rows = (SELECT COUNT(*) FROM @locations)
	
	-- set currunt
	if(@LocationID = 0 or @LocationID is null)
	BEGIN
		SET @LocationID = null
	END
	
	IF(@Rows &gt; 0 and @Rows is not null)
	BEGIN
		print &#039;Row Count = &#039; + CONVERT(NVARCHAR(100), @Rows)
	
		-- find the current row
		SET @Row = (SELECT A.Row FROM @locations as A WHERE A.LocationID = @LocationID)
		print &#039;Row number = &#039; + CONVERT(NVARCHAR(100), @Row)
		
		-- find prev location 
		if(@Row = 1)
		begin
			set @Prev = null
		end
		else
		begin
			set @Prev = (select A.LocationID from @locations as A where A.Row = @Row - 1)
		end
		
		-- find next location
		if(@Rows = 1)
		begin
			set @Next = null
		end
		else
		begin
			set @Next = (select A.LocationID from @locations as A where A.Row = @Row + 1)
		end	
	END
	
	-- Return values
	SELECT PrevLocation = @Prev, CurrentLocation = @LocationID,  NextLocation = @Next
END
GO
EXEC dbo.[GetPrevNext] @CityID = 3022, @LocationID = 6535


--locationIDs for testing
--28941
--20990
--57976
--5744
--6535</description>
		<content:encoded><![CDATA[<p>CREATE PROCEDURE dbo.[GetPrevNext]<br />
	@CityID INT,<br />
	@LocationID INT<br />
AS<br />
DECLARE<br />
	@Prev INT,<br />
	@Next INT,<br />
	@Row  INT,<br />
	@Rows INT<br />
BEGIN<br />
	SET NOCOUNT ON<br />
	DECLARE @locations TABLE (Row INT IDENTITY(1, 1), LocationID INT)</p>
<p>	INSERT @locations<br />
	select<br />
		&#8211; to add and order by enable this<br />
		&#8211; and disable identit on row column in temp table<br />
		&#8211;Row = ROW_NUMBER() OVER (ORDER BY A.LocationID ASC),<br />
		LocationID = A.LocationID<br />
	from<br />
		Gmap_Locations AS A<br />
	where<br />
		A.CityID = @CityID</p>
<p>	SET NOCOUNT OFF</p>
<p>	SET @Rows = (SELECT COUNT(*) FROM @locations)</p>
<p>	&#8211; set currunt<br />
	if(@LocationID = 0 or @LocationID is null)<br />
	BEGIN<br />
		SET @LocationID = null<br />
	END</p>
<p>	IF(@Rows &gt; 0 and @Rows is not null)<br />
	BEGIN<br />
		print &#8216;Row Count = &#8216; + CONVERT(NVARCHAR(100), @Rows)</p>
<p>		&#8211; find the current row<br />
		SET @Row = (SELECT A.Row FROM @locations as A WHERE A.LocationID = @LocationID)<br />
		print &#8216;Row number = &#8216; + CONVERT(NVARCHAR(100), @Row)</p>
<p>		&#8211; find prev location<br />
		if(@Row = 1)<br />
		begin<br />
			set @Prev = null<br />
		end<br />
		else<br />
		begin<br />
			set @Prev = (select A.LocationID from @locations as A where A.Row = @Row &#8211; 1)<br />
		end</p>
<p>		&#8211; find next location<br />
		if(@Rows = 1)<br />
		begin<br />
			set @Next = null<br />
		end<br />
		else<br />
		begin<br />
			set @Next = (select A.LocationID from @locations as A where A.Row = @Row + 1)<br />
		end<br />
	END</p>
<p>	&#8211; Return values<br />
	SELECT PrevLocation = @Prev, CurrentLocation = @LocationID,  NextLocation = @Next<br />
END<br />
GO<br />
EXEC dbo.[GetPrevNext] @CityID = 3022, @LocationID = 6535</p>
<p>&#8211;locationIDs for testing<br />
&#8211;28941<br />
&#8211;20990<br />
&#8211;57976<br />
&#8211;5744<br />
&#8211;6535</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jen</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-15963</link>
		<dc:creator>Jen</dc:creator>
		<pubDate>Tue, 10 Mar 2009 23:21:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=141#comment-15963</guid>
		<description>I have another similar situation but I can&#039;t seem to add the extra columns in.  I have a table with column id, cost, date.  I want to pull the previous record for that id on that date.  Basically, I want to bring in the id, cost date column and then add 2 more columns such as prev date and prev cost.

Example:

Id    Date     Cost  PrevDate   PrevCost
123   3/2/09   $14   2/2/09     $12
123   2/2/09   $12   1/2/09     $10
123   1/2/09   $10
167   3/3/09   $20   1/2/09     $15
167   1/2/09   $15

Thanks very much for any help.</description>
		<content:encoded><![CDATA[<p>I have another similar situation but I can&#8217;t seem to add the extra columns in.  I have a table with column id, cost, date.  I want to pull the previous record for that id on that date.  Basically, I want to bring in the id, cost date column and then add 2 more columns such as prev date and prev cost.</p>
<p>Example:</p>
<p>Id    Date     Cost  PrevDate   PrevCost<br />
123   3/2/09   $14   2/2/09     $12<br />
123   2/2/09   $12   1/2/09     $10<br />
123   1/2/09   $10<br />
167   3/3/09   $20   1/2/09     $15<br />
167   1/2/09   $15</p>
<p>Thanks very much for any help.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Dan</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-15948</link>
		<dc:creator>Dan</dc:creator>
		<pubDate>Sun, 08 Mar 2009 17:38:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=141#comment-15948</guid>
		<description>Xaprb; great solution! I was working on a similiar query for a PHP Image gallery; while sucessful, I was using subqueries and etc. It is truly a pleasure to see ppl still cutting SQL to this degree of elegance! Wish I&#039;d seen this article before :)

Cheers</description>
		<content:encoded><![CDATA[<p>Xaprb; great solution! I was working on a similiar query for a PHP Image gallery; while sucessful, I was using subqueries and etc. It is truly a pleasure to see ppl still cutting SQL to this degree of elegance! Wish I&#8217;d seen this article before :)</p>
<p>Cheers</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Francesco</title>
		<link>http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-15178</link>
		<dc:creator>Francesco</dc:creator>
		<pubDate>Thu, 09 Oct 2008 12:42:52 +0000</pubDate>
		<guid isPermaLink="false">http://www.xaprb.com/blog/?p=141#comment-15178</guid>
		<description>Luckly I bookmarked this topic.

Man, I&#039;d be very grateful if you could help me to adpat this solution to my table, even in MySQL 5+, I&#039;m not very familiar wity queries.

The table:

id (primary key unsigned id)
dt (datetime)

The order of the elements (sign) is by &quot;dt&quot;.

I&#039;d like to get next/prev id&#039;s given an id, based upon &quot;dt&quot; sorting.

Example:

id &#124; dt
-------------------------
1 &#124; &#039;2007-09-17 17:20:00&#039;
2 &#124; &#039;2007-09-17 17:22:00&#039;
3 &#124; &#039;2007-09-17 17:10:00&#039;
4 &#124; &#039;2007-09-17 17:28:00&#039;
5 &#124; &#039;2007-09-17 17:27:00&#039;

Thanks very much for any help.</description>
		<content:encoded><![CDATA[<p>Luckly I bookmarked this topic.</p>
<p>Man, I&#8217;d be very grateful if you could help me to adpat this solution to my table, even in MySQL 5+, I&#8217;m not very familiar wity queries.</p>
<p>The table:</p>
<p>id (primary key unsigned id)<br />
dt (datetime)</p>
<p>The order of the elements (sign) is by &#8220;dt&#8221;.</p>
<p>I&#8217;d like to get next/prev id&#8217;s given an id, based upon &#8220;dt&#8221; sorting.</p>
<p>Example:</p>
<p>id | dt<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
1 | &#8217;2007-09-17 17:20:00&#8242;<br />
2 | &#8217;2007-09-17 17:22:00&#8242;<br />
3 | &#8217;2007-09-17 17:10:00&#8242;<br />
4 | &#8217;2007-09-17 17:28:00&#8242;<br />
5 | &#8217;2007-09-17 17:27:00&#8242;</p>
<p>Thanks very much for any help.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

