Healthcare Analytics Path · Mission 23 of 25Expert

Top diagnoses year-over-year

Use CTEs, LAG with PARTITION BY, and TRIM for year-over-year analysis

Back to Healthcare Analytics

The Brief

Dr. Linda NguyenChief Medical Officerclinical-analytics

For the top 10 diagnosis codes by total volume: show the code, description, encounters this year, encounters last year, and year-over-year change as a percentage. Clean up trailing spaces in diagnosis codes — the EHR issue we flagged before. I want to see if our case mix is shifting.

You'll practice

CTELAG PARTITION BYTRIM

Tables available

fact_encounters

Hints (3)

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

Hint 1

Build a CTE of yearly counts per trimmed diagnosis code. Use EXTRACT to pull the year from admit_date

Hint 2

Use LAG partitioned by diagnosis so each code is compared to its own prior year, not the row above. Then restrict to the top 10 by total volume

Hint 3

`LAG(encounter_count, 1) OVER (PARTITION BY ___ ORDER BY ___) AS prior_year` — plus a top-10 filter using the combined totals across years