SQL Playground Mission: Rolling Sessions Report
Build a daily sessions table, then calculate a three-active-date rolling average with a window frame.
Rolling reports are two-layer queries. First, aggregate the raw events to the reporting grain. Then run the window function over that grouped result.
This mission uses active session dates only, so the rolling average covers the current active date row and the two previous active date rows in the result. It is not a calendar window with missing dates filled in.
with daily as (
select metric_date, count(*) as daily_count
from fact_table
group by metric_date
)
select metric_date,
daily_count,
avg(daily_count) over (
order by metric_date
rows between 2 preceding and current row
) as rolling_average
from dailySchema · Website AnalyticsTable · sessions7 columns · 30 rows
One row per visit. Anonymous visits have a null user_id; organic and direct visits have a null campaign_id.
Return one row per active session_date withsessions, rounded avg_duration, androlling_3_active_day_sessions. The rolling value is the average of sessions over the current row and two preceding rows, ordered by date.
- Columns: session_date, sessions, avg_duration, rolling_3_active_day_sessions.
- Rows: one row per active session date (26 rows).
- The rolling metric averages the current active day and the two prior active days.
You finished “Guided Playground Missions.”
Nice work. Ready to start the next one?
Start Chapter 18: Modeling and Data Quality →Begins with 18.1 Profile a table before you query