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_daydaunew_usersreturning_users
2024-04-0112401801060
2024-04-0213152051110
2024-04-0312881641124
2024-04-0413922211171
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_daydaulatest_dau
2024-04-0112401392
2024-04-0213151392
2024-04-0312881392
2024-04-0413921392

UNBOUNDED FOLLOWING makes the final partition value visible to every row.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario