Provider Analytics Path · Mission 11 of 25Medium

Average length of stay by department

Use date arithmetic with NULL handling and GROUP BY

Back to Provider Analytics

The Brief

James HartwellVP of Operationsslack-dm

need avg length of stay in days by department for inpatient encounters only. exclude anyone still admitted (null discharge). round to 1 decimal. capacity planning meeting in 20 min.

You'll practice

Date arithmeticNULL handling

Tables & columns available

fact_encountersfact10 columns
ColumnTypeKey
encounter_idINTPK
patient_idINTFK → dim_patients
provider_idINTFK → dim_providers
department_idINTFK → dim_departments
admit_dateTEXT
discharge_dateTEXT
diagnosis_codeTEXT
diagnosis_descriptionTEXT
encounter_typeTEXT
discharge_dispositionTEXT
dim_departmentsdim6 columns
ColumnTypeKey
department_idINTPK
department_nameTEXT
department_typeTEXT
floorINT
bed_countINT
manager_nameTEXT

Hints (3)

Each hint you reveal reduces the XP you can earn. Try the query first.

Hint 1

Dates are stored as TEXT — you'll need to cast them to real dates before subtracting. LOS in days is one date minus the other

Hint 2

Filter rows first (inpatient only, discharged only), then aggregate with AVG grouped by department. Round the output

Hint 3

`SELECT d.department_name, ROUND(AVG(e.discharge_date::date - e.admit_date::date), ___) AS avg_los FROM fact_encounters e JOIN dim_departments d ON ___ WHERE e.encounter_type = ___ AND e.discharge_date IS NOT ___ GROUP BY ___`