Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Running Average Over 30 Days
00:00
5 left

Running Average Over 30 Days

MediumSQL · PostgreSQL

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.

The window covers the current usage date and the 29 calendar days before it; dates with no usage rows are simply absent from the window (the average is over the dates that have data).

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
Interviewer

Your question is Running Average Over 30 Days. Start with the requirements and the two 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
Sign up free to run your codeLog inLn 1
Run your query to see results here.