Write a SQL query using window functions to calculate each patient's rolling 30-day event count and rank the most recent events.
Return one row for every recorded event. The rolling count includes the event itself and other events for the same patient from the preceding 30 days through the event timestamp. Rank events separately for each patient by event timestamp descending, allowing events at the same timestamp to share a rank.
Output
- Return
patient_id, patient_name, event_id, event_timestamp, event_type_name, rolling_30_day_event_count, and recency_rank.
- Sort by
patient_id, recency_rank, then event_id.