Use CTEs and self-joins for CMS-style readmission methodology
Build me a readmission analysis using CTEs. Step 1: identify all inpatient discharges (exclude current admits). Step 2: flag any where the same patient had another inpatient admission within 30 days of discharge. Step 3: group by insurance type (normalized with LOWER) and show total discharges, readmissions, and rate as a percentage. This is the methodology CMS uses for HRRP.
Each hint you reveal reduces the XP you can earn. Try the query first.
Build this in stages: CTE 1 holds inpatient discharges. A self-join on that CTE finds cases where the same patient returned within 30 days
The second admission must be AFTER the first discharge AND within 30 days. And make sure you're not matching a discharge to itself
`ON a.patient_id = b.patient_id AND b.admit_date > ___ AND b.admit_date <= ___ + INTERVAL '30 days' AND a.encounter_id ___ b.encounter_id` — then `GROUP BY LOWER(___)`