LAG and LEAD
suggest changeThe LAG function provides data on rows before the current row in the same result set. For example, in a SELECT statement, you can compare values in the current row with values in a previous row.
You use a scalar expression to specify the values that should be compared. The offset parameter is the number of rows before the current row that will be used in the comparison. If you don’t specify the number of rows, the default value of one row is used.
The default is returned when the offset falls off the partition. It is not a substitute for a NULL stored in that row. If you omit it, NULL is returned. The offset defaults to 1.
The LEAD function provides data on rows after the current row in the row set. For example, in a SELECT statement, you can compare values in the current row with values in the following row.
You specify the values that should be compared using a scalar expression. The offset parameter is the number of rows after the current row that will be used in the comparison.
The default is used when there is no row at that offset, not when that row’s value is NULL. If you omit the offset and the default, the offset is one row and a missing row yields NULL.
SELECT BusinessEntityID, SalesYTD,
LEAD(SalesYTD, 1, 0) OVER(ORDER BY BusinessEntityID) AS "Lead value",
LAG(SalesYTD, 1, 0) OVER(ORDER BY BusinessEntityID) AS "Lag value"
FROM SalesPerson;
This example uses the LEAD and LAG functions to compare the sales values for each employee to date with those of the employees listed above and below, with records ordered based on the BusinessEntityID column.
BusinessEntityID | SalesYTD |Lead value|Lag value|
— | ——: |—–:|––:|
274|559697.5639|3763178.1787|0.0000|
275|3763178.1787 |4251368.5497|559697.5639|
276|4251368.5497 |3189418.3662|3763178.1787|
277|3189418.3662 |1453719.4653|4251368.5497|
278|1453719.4653 |2315185.6110|3189418.3662|
279|2315185.6110 |1352577.1325|1453719.4653|