Comments on: How to find next and previous records in SQL http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/ Stay curious! Thu, 02 May 2013 12:36:53 +0000 hourly 1 http://wordpress.org/?v=3.5.1 By: Feroz http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-17827 Feroz Tue, 16 Feb 2010 23:53:25 +0000 http://www.xaprb.com/blog/?p=141#comment-17827 SELECT id FROM table_name WHERE primarykey 5 ORDER BY ID LIMIT 1

]]>
By: tSQL http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-16751 tSQL Wed, 12 Aug 2009 13:25:30 +0000 http://www.xaprb.com/blog/?p=141#comment-16751 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 > 0 and @Rows is not null)
BEGIN
print ‘Row Count = ‘ + CONVERT(NVARCHAR(100), @Rows)

– find the current row
SET @Row = (SELECT A.Row FROM @locations as A WHERE A.LocationID = @LocationID)
print ‘Row number = ‘ + 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

]]>
By: Jen http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-15963 Jen Tue, 10 Mar 2009 23:21:11 +0000 http://www.xaprb.com/blog/?p=141#comment-15963 I have another similar situation but I can’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.

]]>
By: Dan http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-15948 Dan Sun, 08 Mar 2009 17:38:41 +0000 http://www.xaprb.com/blog/?p=141#comment-15948 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’d seen this article before :)

Cheers

]]>
By: Francesco http://www.xaprb.com/blog/2006/04/28/how-to-find-next-and-previous-records-in-sql/#comment-15178 Francesco Thu, 09 Oct 2008 12:42:52 +0000 http://www.xaprb.com/blog/?p=141#comment-15178 Luckly I bookmarked this topic.

Man, I’d be very grateful if you could help me to adpat this solution to my table, even in MySQL 5+, I’m not very familiar wity queries.

The table:

id (primary key unsigned id)
dt (datetime)

The order of the elements (sign) is by “dt”.

I’d like to get next/prev id’s given an id, based upon “dt” sorting.

Example:

id | dt
————————-
1 | ’2007-09-17 17:20:00′
2 | ’2007-09-17 17:22:00′
3 | ’2007-09-17 17:10:00′
4 | ’2007-09-17 17:28:00′
5 | ’2007-09-17 17:27:00′

Thanks very much for any help.

]]>