Guided Notes

Window Functions & 30-Day Readmissions

ROW_NUMBER, RANK, LAG and LEAD with PARTITION BY — and using them to build a 30-day readmission analysis, first/last events and running totals.

What window functions do

Slide 1 / 5

Window functions calculate across related rows without collapsing them (unlike GROUP BY): function() OVER (PARTITION BY patient_id ORDER BY admit_date) • PARTITION BY — restart for each patient. • ORDER BY — the order inside each patient. Perfect for 'next visit', 'previous result', 'first diagnosis', 'running total' questions that healthcare is full of.

← CASE, NULLs & Cleaning in SQL