runtime
Video Product Analytics · milestone 02 / 04

milestone 2/4 Rolling averages & missing dates

Calculate rolling trends while accounting for missing dates.

Video Product Analytics · Move from watch events to daily engagement, rolling trends, and signup-cohort retention.

approach

A rolling average smooths variation. ROWS counts rows, not calendar days. When dates are missing, a three-row average differs from a three-day average. Fill missing days with zero only if the business definition says absence means no activity.

worked-example.sql
SELECT
  day,
  watch_minutes,
  ROUND(AVG(watch_minutes) OVER( ORDER BY day ROWS BETWEEN 2 PRECEDING AND CURRENT ROW ),2) AS rolling_avg
FROM daily_metrics;

data for this milestone

daily_metrics2 columns · 5 rows
daytext · keywatch_minutesinteger
2025-01-0130
2025-01-0260
2025-01-0490
2025-01-05120
2025-01-0760

all 5 rows

challenge 1

Return day, watch_minutes and rolling_avg (2 decimals) for the current and previous 2 observed rows in daily_metrics, sorted by day. This task intentionally uses rows, not calendar days.

Return daywatch_minutesrolling_avgrows in the order asked

challenge-1.sql
 
3 steps left · check both queries and pass the concept checkNext milestone
Rolling averages & missing dates — Video Product Analytics · Runtime