filmov
tv
Getting the previous or next row in SQL Server (T-SQL) #sqlserver #shorts #sql
Показать описание
How can you get the next or the previous row in SQL Server?
My SQL Server Udemy courses are:
----
You can use the LEAD (to get the next row) or LAG (to get the previous row). You also need an OVER which must include, at the very list, an ORDER BY.
You can also use a second argument to get the 2nd next row, like this:
SELECT object_id, [name]
, LEAD(name, 2) OVER (ORDER BY object_id) AS NextName
, LAG(name) OVER (ORDER BY object_id) AS PreviousName
ORDER BY object_id
----
Links to my website are:
My SQL Server Udemy courses are:
----
You can use the LEAD (to get the next row) or LAG (to get the previous row). You also need an OVER which must include, at the very list, an ORDER BY.
You can also use a second argument to get the 2nd next row, like this:
SELECT object_id, [name]
, LEAD(name, 2) OVER (ORDER BY object_id) AS NextName
, LAG(name) OVER (ORDER BY object_id) AS PreviousName
ORDER BY object_id
----
Links to my website are: