Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Running Average Over 30 Days

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

Your question is Running Average Over 30 Days. 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

ResMed's therapy analytics team wants to monitor patient adherence for ResMed AirSense 11 users. Write a PostgreSQL query that calculates each patient's running average daily usage over the previous 30 calendar days, including the current usage date.

Requirements

  1. Include only patients using ResMed AirSense 11.
  2. Aggregate multiple usage records for the same patient and date before applying the window function.
  3. Calculate a 30-day running average partitioned by patient and ordered chronologically.
  4. Return the patient code, usage date, daily usage, and running average, ordered by patient and date. Null usage values should be ignored by AVG.

Schema

patients
ColumnTypeDescription
patient_idPKINTUnique patient identifier
patient_codeVARCHAR(20)De-identified patient code
device_modelVARCHAR(50)ResMed therapy device model
patient_usage
ColumnTypeDescription
usage_idPKINTUnique usage record identifier
patient_idINTReferences patients.patient_id
usage_dateDATEDate of therapy usage
usage_hoursNUMERIC(5,2)Therapy usage hours recorded for the date
Tablespatientspatient_usage
Your solutionPostgreSQL
You need to log in / sign up to run or submit.
Run a query to see results