Avatar of John Smith
John Smith
Flag for Gibraltar asked on

SQL - Previous/Next Records

I have a table with a GUID ID column and Firstname, Surname fields. I want to be able to select a record using the GUID and also select the previous/next record sorted by the Firstname and Surname fields. So for example, i

Capture2.PNG
In the example above, I want to select record c5875.... (Kevin) and have the previous/next sorted by the 2nd/3rd column and have it display John (prev) and Tracy (next).

Hope that makes sense.
Microsoft SQL Server

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
Olaf Doschke

See LEAD() and LAG(), if you have SQL2008+

Bye, Olaf.
Pawan Kumar

@Olaf Doschke - LEAD() and LAG() were available from SQL Server 2012.
https://msdn.microsoft.com/en-IN/library/hh213125.aspx


SQL Server 2012 introduces new analytical function LEAD() and LAG(). These functions accesses data from a subsequent row (for lead) and previous row (for lag) in the same result set without the use of a self-join .

NEXT Value Sample example - https://msbiskills.com/2012/07/19/t-sql-2/

Previous Value Sample example - https://msbiskills.com/2012/07/19/t-sql-1/

Enjoy !
Sharath S

To complete Olaf's suggestion.
select *,
       lag(FirstName) over (order by FirstName) Prev,
	   lead(FirstName) over (order by FirstName) Nxt
  from your_table

Open in new window

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Olaf Doschke

Sorry, yes, wrong promise. It's 2012+ only.

Bye, Olaf.
John Smith

ASKER
Thanks for the responses. That is giving me null values for some reason.
Vitor Montalvão

I think the requirement need clarifications:
  • What's happening if you have two Johns or Tracys?
  • And if you want to select Anne (first record) or Tracy (last record)?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
John Smith

ASKER
Thanks for the response Vitor. I'm selecting the record by GUID, but want to include the previous / next records based on the Firstname, Surname values.
Olaf Doschke

Well, LEAD and LAG only give one sclara value (expression), usually one column of the previous or next row, not the full previous/next row. So you get the next and previous lastname in two extra columns, not as separate rows.

You could query the guids you need to query:
select GUID,
       lag(guid) over (order by FirstName, SurName) PrevGUID,
	   lead(guid) over (order by FirstName, SurName) NxtGUID
  from your_table WHERE GUID = '...'

Open in new window


Bye, Olaf.
Vitor Montalvão

I understand you're selecting by GUID but you're getting the previous and next records based in the firstname order and not in the GUID.
Like, Tracy's GUID is not the next one after Kevin's (it should by Geoff's by your example).
Your help has saved me hundreds of hours of internet surfing.
fblack61
Sharath S

>> That is giving me null values for some reason.
post some sample data where you are getting NULLs.
Olaf Doschke

You get NULLS because the main query only fetches one row, you can only get next and previous rows, if the result has more than the one row, that's the problem here.

This is going round in circles, if you first don't know the next and previous neighbors you have to row_number all and then you might also fetch all anyway, and scroll to the one guid.

Bye, Olaf.
ASKER CERTIFIED SOLUTION
Olaf Doschke

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
John Smith

ASKER
I'll try that and revert.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Vitor Montalvão

John, any update of this question?
Pawan Kumar

Hey John, Just wanted to check, have you checked code/post i mentioned.?