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.
ResMed AirSense 11.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).
| Column | Type | Description |
|---|---|---|
| patient_idPK | INT | Unique patient identifier |
| patient_code | VARCHAR(20) | De-identified patient code |
| device_model | VARCHAR(50) | ResMed therapy device model |
| Column | Type | Description |
|---|---|---|
| usage_idPK | INT | Unique usage record identifier |
| patient_id | INT | References patients.patient_id |
| usage_date | DATE | Date of therapy usage |
| usage_hours | NUMERIC(5,2) | Therapy usage hours recorded for the date |