LAST_VALUE
Takes the value from the last row of the window — but only with the right frame.
Syntax
LAST_VALUE(<expression>) OVER (
ORDER BY <sort key>
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)
LAST_VALUE(<expression>) OVER (
PARTITION BY <group> ORDER BY <sort key>
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
)
Adds to every row the value from the last row of its window. The key detail is the frame: by default the window ends at the current row, so without ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING the “last” row is the current row itself. Its mirror function is FIRST_VALUE.