At many instances, user would like to access data of the previous row or any row before the previous row from the current row.
To solve this problem SQL Server’s LAG() window function can be used.
SQL Server provides LAG() function which is very useful in case the current row values need to be compared with the data/value of the previous record or any record before the previous record. The previous value can be returned on the same record without the use of self join making it straightforward to compare.
LAG (scalar_expression [, offset] [, default]) OVER ( [ partition_by ] order_by )
- scalar_expression –
The value to be returned based on the specified offset.
- offset –
The number of rows back from the current row from which to obtain a value. If not specified, the default is 1.
- default –
default is the value to be returned if offset goes beyond the scope of the partition. If a default value is not specified, NULL is returned.
- over ( [ partition_by] order_by) –
partition_by divides the result set produced by the FROM clause into partitions to which the function is applied. If you omit PARTITION BY clause, the function treats whole result set as a single group. By default order_by clause sorts in ascending order.
SELECT Organisation, [Year], Revenue, LAG (Revenue, 1, 0) OVER (PARTITION BY Organisation ORDER BY [Year]) AS PrevYearRevenue FROM Org ORDER BY Organisation, [Year];
In the above example, We have 2 TV News Channel whose Current and Previous Year’s Revenue is presented on the same row using the LAG() function. As You can see that the very first record for each of the TV News channels don’t have previous year revenues so it shows the default value of 0. This function can be very useful in yielding data for BI reports when you want to compare values in consecutive periods, for e.g. Year on Year or Quarter on Quarter or Daily Comparisons.
SELECT Z.*, (Z.Revenue - z.PrevYearRevenue) as YearonYearGrowth from (SELECT Organisation, [Year], Revenue, LAG (Revenue, 1) OVER (PARTITION BY Organisation ORDER BY [Year] ) AS PrevYearRevenue FROM Org) Z ORDER BY Organisation, [Year];
In the above example, We can similarly calculate Year On Year Growth for the TV News Channel. Also, one thing to notice in this example is we haven’t supplied any default parameter to LAG(), and hence the LAG() function returns NULL in case there are no previous values.
The LAG() function can be implemented at the database level and BI Reporting solutions like Power BI and Tableau can avoid using the cumbersome measures at the reporting layer.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- SQL Server LEAD() function Overview
- MySQL | LEAD() and LAG() Function
- Relational Overview Vs Cassandra Overview
- Difference between Structured Query Language (SQL) and Transact-SQL (T-SQL)
- SQL Server | STUFF() Function
- SIN() and COS() Function in SQL Server
- RANK() Function in SQL Server
- NTILE() Function in SQL Server
- STR() Function in SQL Server
- UNICODE() Function in SQL Server
- SUBSTRING() Function in SQL Server
- RTRIM() Function in SQL Server
- FLOOR() and CEILING() Function in SQL Server
- TAN() and COT() Function in SQL Server
- DEGREES() and RADIANS() Function in SQL Server
- ATN2() Function in SQL Server
- LOG10() Function in SQL Server
- LOG() Function in SQL Server
- ASCII() Function in SQL Server
- DIFFERENCE() Function in SQL Server
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.