Use EXTRACT(DOW), AVG with filtering, and a window function for baseline comparison
avg ED wait time by day of week. exclude the bad data over 480 min. also show an overall average across all days as a baseline column so i can see which days are above/below average. weekends are killing us — need proof.
| Column | Type | Key |
|---|---|---|
| metric_id | INT | PK |
| encounter_id | INT | FK → fact_encounters |
| patient_id | INT | FK → dim_patients |
| metric_type | TEXT | |
| metric_value | REAL | |
| measurement_date | TEXT | |
| reported_by | TEXT |
| Column | Type | Key |
|---|---|---|
| encounter_id | INT | PK |
| patient_id | INT | FK → dim_patients |
| provider_id | INT | FK → dim_providers |
| department_id | INT | FK → dim_departments |
| admit_date | TEXT | |
| discharge_date | TEXT | |
| diagnosis_code | TEXT | |
| diagnosis_description | TEXT | |
| encounter_type | TEXT | |
| discharge_disposition | TEXT |
| Column | Type | Key |
|---|---|---|
| department_id | INT | PK |
| department_name | TEXT | |
| department_type | TEXT | |
| floor | INT | |
| bed_count | INT | |
| manager_name | TEXT |
Each hint you reveal reduces the XP you can earn. Try the query first.
Two data quality issues compound: the three ED name variants AND the impossible wait times over 480. Filter both before you aggregate
Group by day-of-week with `EXTRACT`, and add a window function with no partition to put the grand average on every row
`SELECT EXTRACT(___ FROM measurement_date::date) AS day_of_week, AVG(metric_value) AS avg_wait, AVG(AVG(metric_value)) OVER () AS overall_avg ... WHERE metric_value <= ___ AND department_name IN (___, ___, ___) GROUP BY ___`