LAG
Description
Sample Source Pattern
Syntax
LAG (scalar_expression [,offset] [,default])
OVER ( [ partition_by_clause ] order_by_clause ) COUNT( [ DISTINCT ] <expr1> [ , <expr2> ... ] )Examples
SELECT TOP 10
LAG(E.VacationHours,1) OVER(ORDER BY E.NationalIdNumber) as PREVIOUS,
E.VacationHours AS ACTUAL
FROM HumanResources.Employee EPREVIOUS|ACTUAL|
--------+------+
NULL| 10|
10| 89|
89| 10|
10| 48|
48| 0|
0| 95|
95| 55|
55| 67|
67| 84|
84| 85|SELECT TOP 10 LAG(E.VacationHours,1)
OVER(ORDER BY E.NationalIdNumber) as PREVIOUS,
E.VacationHours AS ACTUAL
FROM HumanResources.Employee E;PREVIOUS|ACTUAL|
--------+------+
NULL| 10|
10| 89|
89| 10|
10| 48|
48| 0|
0| 95|
95| 55|
55| 67|
67| 84|
84| 85|Last updated
Was this helpful?