LAST_VALUE
window analytic
What changes
- Input
- ordered rows of the partition (frame for FIRST_VALUE/LAST_VALUE; offset rows for LAG/LEAD)
- Output or change
- one value per row
- Return type
- The same type as scalar_expression.
Syntax at a glance
LAST_VALUE ( [ scalar_expression ] ) [ IGNORE NULLS | RESPECT NULLS ] OVER ( [ partition_by_clause ] order_by_clause [ rows_range_clause ] )
Work example
Show the final value in a chosen window frame.
Write the frame through UNBOUNDED FOLLOWING when each row should see the partition's final value.
Check your work The default frame often ends at the current peer group, so LAST_VALUE can otherwise mean the current value.
Practice SQL
Open in SQL FlowSELECT n, LAST_VALUE(n) OVER (ORDER BY n ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS last_n FROM (VALUES (20), (10), (30)) AS v(n);
Open Microsoft Learn for full syntax, permissions, and details for your version.