0

I know that I can use LAG and LEAD to get the previous and subsequent rows from a results set. What I want to know is how to get the subsequent row, and the row after that one, and the next row. I don't want to use a cursor if pivot, if possible.

Is it possible to LEAD(n) with n being the number of rows ahead?

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275
Steve_Malcolm
  • 53
  • 1
  • 9
  • 2
    Documentation is your friend: [LEAD (Transact-SQL)](https://learn.microsoft.com/en-us/sql/t-sql/functions/lead-transact-sql?view=sql-server-2017). Specifically, look at the Arguments section. Learning to read documentation is a very important skill, and i really suggest taking the time to use it if you have any questions about how a function works, or what it can do. – Thom A Oct 04 '18 at 14:39

1 Answers1

7

Yes it is possible:

SELECT LEAD(col_name, offset, default_value) OVER(...)
FROM tab

For example: LEAD(col,1) is default behavior, LEAD(col, 2) - second row ahead

Lukasz Szozda
  • 162,964
  • 23
  • 234
  • 275