Write a query to find the most recent clinical event for each patient using window functions.
Use the patients and clinical_events tables. Return only patients with at least one clinical event. If multiple events share the latest timestamp, select the event with the highest event_id.
patient_id ascending.patient_id, event_id, event_type, and event_timestamp.| Column | Type | Description |
|---|---|---|
| patient_idPK | INT | Unique patient identifier |
| patient_name | VARCHAR(100) | Patient display name |
| Column | Type | Description |
|---|---|---|
| event_idPK | INT | Unique clinical event identifier |
| patient_id | INT | Patient associated with the event |
| event_type | VARCHAR(50) | Clinical event classification |
| event_timestamp | TIMESTAMPTZ | Timestamp when the event occurred |