Healthcare Analytics Path · Mission 11 of 25Medium

Average length of stay by department

Use date arithmetic with NULL handling and GROUP BY

Back to Healthcare 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 available

fact_encountersdim_departments

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 ___`