Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Rolling 7-Day Audience Average

MediumSQL · PostgreSQL00:00
Practice interviewer
In session
5 left
00:00

Your question is Rolling 7-Day Audience Average. Start with the requirements and the two tables on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

The Nielsen needs a daily audience trend for Nielsen measurement regions. Write a PostgreSQL query that calculates a calendar-day rolling average for each region.

Requirements

  1. Aggregate multiple audience observations for the same region and date.
  2. Generate every date in the requested period, treating missing or NULL audience counts as zero.
  3. Calculate a seven-calendar-day average, including the current date and six preceding dates within each region.
  4. Return the region, date, daily audience, and rolling average, rounded to two decimals, ordered by region and date.

Schema

regions
ColumnTypeDescription
region_idPKINTUnique region identifier
region_nameVARCHAR(80)Nielsen measurement region name
audience_observations
ColumnTypeDescription
observation_idPKINTUnique audience observation identifier
region_idINTReferences regions.region_id
audience_dateDATEDate of the audience measurement
audience_countINTObserved audience count
Tablesregionsaudience_observations
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results