Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

SQL 7-Day Rolling Average

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

Your question is SQL 7-Day Rolling 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

Keyrus France's Data Intelligence reporting needs a daily view of conversions from active campaigns. Write a PostgreSQL query for the period from 2024-06-01 through 2024-06-10.

Requirements

  1. Count only events with conversion_status = 'converted' belonging to active campaigns.
  2. Return every calendar date in the period, including dates with zero conversions.
  3. Calculate a seven-day rolling average using the current date and up to the six preceding calendar days.
  4. Return conversion_date, daily_conversions, and rolling_7_day_average, ordered chronologically. Round the average to two decimal places.

Schema

campaigns
ColumnTypeDescription
campaign_idPKINTEGERUnique campaign identifier
campaign_nameVARCHAR(100)Campaign display name
statusVARCHAR(20)Campaign lifecycle status
conversion_events
ColumnTypeDescription
event_idPKINTEGERUnique conversion event identifier
campaign_idINTEGERCampaign associated with the event
converted_atTIMESTAMPTimestamp when the event occurred
conversion_statusVARCHAR(20)Conversion processing outcome
Tablescampaignsconversion_events
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results