In SQL Server, the LAST_VALUE()
function returns the last value in an ordered set of values.
LAST_VALUE()
is a window function that enables us to get a value from the last row of a query result set or partition. This can be useful for when we want to do stuff such as compare a value from the current row with a value in the last row or include it in a calculation.
You may need to explicitly set the window frame if you want LAST_VALUE()
to return the actual last value from the partition or result set. That’s because the default window frame ends with the current row. This is covered in the example below.