In SQL Server, LAG()
is a window function that enables us to access a value from a previous row in the same result set, without the need to perform a self-join.
We specify the previous row as an offset from the current row. An offset of 1
means the previous row, an offset of 2
means two rows back, and so on.