SQL CURRENT ROW

The frame boundary that stops (or starts) exactly at the row being processed.

What & Why

CURRENT ROW as a frame boundary means the frame includes the row being processed right now — no more, no less, at that end.

See How It Works

BUSINESS QUESTION

Growth wants a three-observation active-user average ending on each day.

date_daydaunew_usersreturning_users
2024-04-0112401801060
2024-04-0213152051110
2024-04-0312881641124
2024-04-0413922211171
EXAMPLE QUERY
SELECT
  date_day,
  dau,
  ROUND(AVG(dau) OVER (ORDER BY date_day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 2) AS trailing_avg_dau
FROM growth.daily_active_users
ORDER BY date_day;
RESULT — trailing three-row DAU average
date_daydautrailing_avg_dau
2024-04-0112401240.00
2024-04-0213151277.50
2024-04-0312881281.00
2024-04-0413921331.67

CURRENT ROW makes each result stop at its own observation.

This lesson's practice is part of Pro.

Advanced business practice

Sign up free to try it on a real business scenario