Write a SQL query using window functions to rank patients within each cohort by outcome date and compute a running total of events.
Use the patients and events tables. Include patients without events, place NULL outcome dates last, and use patient ID to make the final ordering deterministic.
patient_id, cohort, outcome_date, event_count, cohort_rank, and running_event_total.| Column | Type | Description |
|---|---|---|
| patient_idPK | INT | Unique patient identifier |
| cohort | VARCHAR(20) | Patient cohort label |
| outcome_date | DATE | Date associated with the patient outcome |
| Column | Type | Description |
|---|---|---|
| event_idPK | INT | Unique event identifier |
| patient_id | INT | Patient identifier recorded for the event |
| event_type | VARCHAR(40) | Type of patient event |
| event_date | DATE | Date on which the event occurred |