SQL LAST_VALUE
Returns the window's last row — but the default frame almost never shows you what you expect.
What & Why
LAST_VALUE(column) should return the final row's value in the window, repeated on every row — the mirror of FIRST_VALUE. In practice, it almost always needs one extra clause to actually behave that way.
See How It Works
BUSINESS QUESTION
Growth shows every DAU row beside the latest DAU in the available series.
| date_day | dau | new_users | returning_users |
|---|---|---|---|
| 2024-04-01 | 1240 | 180 | 1060 |
| 2024-04-02 | 1315 | 205 | 1110 |
| 2024-04-03 | 1288 | 164 | 1124 |
| 2024-04-04 | 1392 | 221 | 1171 |
EXAMPLE QUERY
SELECT
date_day,
dau,
LAST_VALUE(dau) OVER (
ORDER BY date_day
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS latest_dau
FROM growth.daily_active_users
ORDER BY date_day;RESULT — latest DAU across the full frame
| date_day | dau | latest_dau |
|---|---|---|
| 2024-04-01 | 1240 | 1392 |
| 2024-04-02 | 1315 | 1392 |
| 2024-04-03 | 1288 | 1392 |
| 2024-04-04 | 1392 | 1392 |
UNBOUNDED FOLLOWING makes the final partition value visible to every row.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario