Hi,@T.Zacks
Welcome to Microsoft T-SQL Q&A Forum!
The two reference documents you gave already point in the right direction, using the lag function to access a row with a specified physical offset before moving forward. Look this example,
select (ID, Value) from table as table1 join
inner join table as table2
on table1.ID = (table2.ID -1)
if you look at it carefully, just to find the ID number of the previous row, LAG is shown here to make it easier to implement.Naomi also gave the answer, and here I use a subfunction.
Code:
Create table #test
(
ID int identity(1,1),
Quarter nvarchar(20)
)
insert into #test values
('1Q 2010'),
('2Q 2010'),
('3Q 2010'),
('4Q 2010'),
('FY 2010')
select * from #test
select PrevID,PrevQuarter,CurrID,CurrQuarter
from
(
select Lag(ID,1) over(order by ID)PrevID ,LAG(Quarter,1)over(order by ID)PrevQuarter,
ID as CurrID,Quarter as CurrQuarter
from #test
)t
where CurrID=4
->>>>Note that I used parameters in the lag function. If you can test the effect of changing 1 to 2, you will find that you can get the offset of the first two lines.
Best regards,
Bert Zhou
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.