A 30-day readmission is an inpatient admission that starts within 30 days of an earlier inpatient discharge of the same patient. The earlier stay is the index stay, and every ended inpatient stay is one.
LEAD reads each patient's next admission onto every stay:
SELECT encounter_id, patient_id, discharge_date,
LEAD(admit_date) OVER (PARTITION BY patient_id
ORDER BY admit_date) AS next_admit_date
FROM hd_encounters
WHERE encounter_type = 'inpatient';
A stay is readmitted when next_admit_date - discharge_date <= 30. The rate is readmitted index stays over index stays.
The pitfalls: counting the 30 days from the admission instead of the discharge; letting observation stays or emergency visits into the window (an emergency visit that became an admission the same day is one episode, not a discharge and a readmission); filtering out open stays before LEAD, which loses readmissions still in a bed; and integer division. This lesson counts every readmission; the next one sets planned ones aside.
Readmissions within 30 days of an index discharge.