SQL · Pattern 10 — Window Functions
LAST_VALUE()
Returns last value in a window.
Source data — Pattern 10: Window Functions
All queries run against the shared employees table (see schema.sql). This pattern introduces a sales table to demonstrate window functions including ranking, navigation, running totals, moving averages and window aggregates.


What it does
Returns last value in a window.
Problem
Display the last sale in each department.
SELECT *,
LAST_VALUE(sales_amount) OVER(PARTITION BY department_id ORDER BY sale_month
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS result
FROM sales;
Result
