Write a SQL query using window functions to calculate each patient's 7-day rolling average lab value and compare it to their prior week.
Use lab_measurements as the source table. Treat multiple measurements on the same patient and date as one daily average, and preserve dates whose lab values are all NULL.
patient_id, then measurement_date.patient_id, measurement_date, daily_lab_value, rolling_7_day_avg, prior_week_rolling_avg, change_from_prior_week, and comparison.no_prior_week when that date is unavailable.| Column | Type | Description |
|---|---|---|
| measurement_idPK | INT | Unique laboratory measurement identifier |
| patient_id | INT | Patient identifier |
| measured_at | DATE | Date on which the lab value was measured |
| lab_value | NUMERIC(10,2) | Numeric laboratory result |