Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Rolling Averages With Windows
00:00
5 left

Rolling Averages With Windows

MediumSQL · PostgreSQL

Problem

Write a SQL query using SQL window functions to calculate rolling thirty-day active user averages across multiple regional patient registries.

Use the supplied registry, patient, and activity data. Include every registry for every calendar date in the activity range, treating dates without activity as zero active users.

Output

  1. One row per registry and calendar date, including registries with no activity.
  2. Columns: registry_id, registry_name, region_code, activity_date, active_users, and rolling_30_day_average.
  3. Count distinct active patients with activity on each date. Order by registry_id, then activity_date.

Schema

registries
ColumnTypeDescription
registry_idPKINTUnique patient registry identifier
registry_nameVARCHAR(100)Registry name
region_codeVARCHAR(10)Regional code for the registry
patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
registry_idINTRegistry associated with the patient
statusVARCHAR(20)Patient participation status
activity_events
ColumnTypeDescription
event_idPKINTUnique activity event identifier
patient_idINTPatient associated with the activity
activity_dateDATEDate of patient activity
Tablesregistriespatientsactivity_events
Interviewer

Your question is Rolling Averages With Windows. Start with the requirements and the three tables in the Question tab.

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.
CodePostgreSQL
You need to log in / sign up to run or submit.Ln 1
Run your query to see results here.