SQL Server 2012 introduced new analytical function LEAD() and LAG().
These functions accesses data from nth next row and nth previous row in the same result set without the use of a self-join:
- LEAD(): It is used to access data from nth next row in the same result set without the use of a self-join.
- LAG(): It is used to access data from nth previous row in the same result set without the use of a self-join.
Syntax
- LEAD (scalar_expression [,offset] [,default])
- OVER ( [ partition_by_clause ] order_by_clause )
- LAG (scalar_expression [,offset] [,default])
- OVER ( [ partition_by_clause ] order_by_clause )
- scalar_expression: Column name for which value to be accessed.
- offset: Nth previous (for lag) or next (for lead) row to access the column.
- defaul : Default value to display if nth row does not exist.
Example 1
- DECLARE @Test_table TABLE(
- Year INT, Sale INT
- )
- INSERT INTO @Test_table VALUES('2008',5000000)
- INSERT INTO @Test_table VALUES('2009',5500000)
- INSERT INTO @Test_table VALUES('2010',5250000)
- INSERT INTO @Test_table VALUES('2011',6025000)
- INSERT INTO @Test_table VALUES('2012',6200000)
- SELECT Year, Sale
- , LEAD(Sale) OVER (ORDER BY Year) AS [Next Year Sale]
- , LAG(Sale) OVER (ORDER BY Year) AS [Prev Year Sale]
- , LEAD(Sale, 2) OVER (ORDER BY Year) AS [2nd Next Year Sale]
- , LAG(Sale, 2) OVER (ORDER BY Year) AS [2nd Prev Year Sale]
- , LEAD(Sale, 2, 0) OVER (ORDER BY Year) AS [2nd Next Year Sale]
- , LAG(Sale, 2, 0) OVER (ORDER BY Year) AS [2nd Prev Year Sale]
- FROM @Test_table

Example 2 (With Partition By)
- DECLARE @Test_table TABLE(
- Year INT, Zone VARCHAR(10), Sale INT
- )
- INSERT INTO @Test_table VALUES('2009', 'East', 5500000)
- INSERT INTO @Test_table VALUES('2010', 'East', 5250000)
- INSERT INTO @Test_table VALUES('2011', 'East', 6025000)
- INSERT INTO @Test_table VALUES('2012', 'East', 6200000)
- INSERT INTO @Test_table VALUES('2009', 'West', 5200000)
- INSERT INTO @Test_table VALUES('2010', 'West', 5250000)
- INSERT INTO @Test_table VALUES('2011', 'West', 5525000)
- INSERT INTO @Test_table VALUES('2012', 'West', 5700000)
- INSERT INTO @Test_table VALUES('2009', 'North', 4700000)
- INSERT INTO @Test_table VALUES('2010', 'North', 4800000)
- INSERT INTO @Test_table VALUES('2011', 'North', 5000000)
- INSERT INTO @Test_table VALUES('2012', 'North', 5050000)
- INSERT INTO @Test_table VALUES('2009', 'South', 7200000)
- INSERT INTO @Test_table VALUES('2010', 'South', 7500000)
- INSERT INTO @Test_table VALUES('2011', 'South', 7800000)
- INSERT INTO @Test_table VALUES('2012', 'South', 8000000)
- SELECT Year, Sale
- , LEAD(Sale) OVER (PARTITION BY Zone ORDER BY Year) AS [Next Year Sale]
- , LAG(Sale) OVER (PARTITION BY Zone ORDER BY Year) AS [Prev Year Sale]
- , LEAD(Sale, 2) OVER (PARTITION BY Zone ORDER BY Year) AS [2nd Next Year Sale]
- , LAG(Sale, 2) OVER (PARTITION BY Zone ORDER BY Year) AS [2nd Prev Year Sale]
- , LEAD(Sale, 2, 0) OVER (PARTITION BY Zone ORDER BY Year) AS [2nd Next Year Sale]
- , LAG(Sale, 2, 0) OVER (PARTITION BY Zone ORDER BY Year) AS [2nd Prev Year Sale]
- FROM @Test_table
Sridhar SharmaPosted Nov 20, 2015, 11:28 AM
Nice Share
Yashwant VishwakarmaPosted Nov 20, 2015, 9:17 AM
Nice article dude !!!
Santhakumar MunuswamyPosted Nov 20, 2015, 8:59 AM
Good One
Banketeshvar NarayanPosted Nov 20, 2015, 8:34 AM
Good One
Rajeesh MenothPosted Nov 20, 2015, 7:36 AM
Thanks for sharing!