SQL · Pattern 10 — Window Functions
FIRST_VALUE()
Returns first 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 first value in a window.
Problem
Display the first sale in each department.
SELECT *,
FIRST_VALUE(sales_amount) OVER(PARTITION BY department_id ORDER BY
sale_month) AS result
FROM sales;
Result
