Skip to content
/Chapter 17 · Guided Playground Missions
Lesson 17.8·website_analytics
Lesson 17.8

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.

Pattern
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 daily
Schema · Website AnalyticsTable · sessions7 columns · 30 rows
Table · sessions

One row per visit. Anonymous visits have a null user_id; organic and direct visits have a null campaign_id.

7 columns · 30 rows
session_id intuser_id intsession_date datechannel textcampaign_id intdevice textduration_seconds int
Your task

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.

SQL Workbench
query.sqlwebsite_analytics · SQL engine loading
⌘↵ to run
·
Expected answer
  • 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.
← Previous · 17.7 Campaign revenue report
✓ Chapter 17 complete

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