October 11, 2026
SQL notes · clinical toy data · per-group calculations without collapsing rows
The idea. Aggregates like AVG collapse rows; window
functions don't. Each row keeps its identity while the function peers at a
window of related rows — the patient's other visits, the previous reading,
a running total. One query, row-level detail and group math.
SELECT patient_id, reading_date, hba1c,
FUNC(...) OVER (
PARTITION BY patient_id -- restart per patient ("per category")
ORDER BY reading_date -- the sequence within each group
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW -- the frame: which rows count
)
FROM a1c_readings;
PARTITION BY is "per category": restart the calculation for each
patient (or ward, or drug). ORDER BY defines the sequence. The frame
(ROWS BETWEEN …) says which rows count — omit it and you get the
default, which is rarely what you want for rolling math.
a1c_readings — HbA1c lab readings per patient over time
| patient_id | reading_date | hba1c |
|---|---|---|
| 1 | 2024-01-05 | 8.2 |
| 1 | 2024-04-02 | 7.9 |
| 1 | 2024-07-10 | 7.9 |
| 1 | 2024-10-01 | 7.4 |
| 2 | 2024-02-11 | 9.1 |
| 2 | 2024-05-20 | 8.8 |
med_admin — medication doses administered
| patient_id | admin_date | dose_mg |
|---|---|---|
| 1 | 2024-01-05 | 500.0 |
| 1 | 2024-02-05 | 500.0 |
| 1 | 2024-03-05 | 250.0 |
| 2 | 2024-02-11 | 850.0 |
SELECT patient_id, reading_date,
ROW_NUMBER() OVER (PARTITION BY patient_id ORDER BY reading_date) AS visit_n
FROM a1c_readings ORDER BY patient_id, reading_date;
| patient_id | reading_date | visit_n |
|---|---|---|
| 1 | 2024-01-05 | 1 |
| 1 | 2024-04-02 | 2 |
| 1 | 2024-07-10 | 3 |
| 1 | 2024-10-01 | 4 |
| 2 | 2024-02-11 | 1 |
| 2 | 2024-05-20 | 2 |
SELECT patient_id, hba1c,
RANK() OVER (PARTITION BY patient_id ORDER BY hba1c DESC) AS rnk,
DENSE_RANK() OVER (PARTITION BY patient_id ORDER BY hba1c DESC) AS dense_rnk
FROM a1c_readings ORDER BY patient_id, hba1c DESC;
| patient_id | hba1c | rnk | dense_rnk |
|---|---|---|---|
| 1 | 8.2 | 1 | 1 |
| 1 | 7.9 | 2 | 2 |
| 1 | 7.9 | 2 | 2 |
| 1 | 7.4 | 4 | 3 |
| 2 | 9.1 | 1 | 1 |
| 2 | 8.8 | 2 | 2 |
Patient 1 has two readings of 7.9 (a tie). RANK() gives both rank 2
then skips to 4; DENSE_RANK() doesn't skip (2, 2, 3). Pick
based on whether the skipped numbers matter downstream.
SELECT patient_id, reading_date, hba1c,
ROUND(hba1c - LAG(hba1c) OVER (PARTITION BY patient_id ORDER BY reading_date), 2)
AS change_since_last
FROM a1c_readings ORDER BY patient_id, reading_date;
| patient_id | reading_date | hba1c | change_since_last |
|---|---|---|---|
| 1 | 2024-01-05 | 8.2 | NULL |
| 1 | 2024-04-02 | 7.9 | -0.3 |
| 1 | 2024-07-10 | 7.9 | 0.0 |
| 1 | 2024-10-01 | 7.4 | -0.5 |
| 2 | 2024-02-11 | 9.1 | NULL |
| 2 | 2024-05-20 | 8.8 | -0.3 |
LAG(hba1c) is "the previous row's value, within this patient's
sequence." The first row per patient has no previous row → NULL,
honestly. LEAD() is the mirror image (next row).
SELECT patient_id, reading_date, hba1c,
ROUND(AVG(hba1c) OVER (PARTITION BY patient_id ORDER BY reading_date
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 2)
AS rolling_avg_3,
COUNT(*) OVER (PARTITION BY patient_id ORDER BY reading_date
ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS n_in_avg
FROM a1c_readings ORDER BY patient_id, reading_date;
| patient_id | reading_date | hba1c | rolling_avg_3 | n_in_avg |
|---|---|---|---|---|
| 1 | 2024-01-05 | 8.2 | 8.2 | 1 |
| 1 | 2024-04-02 | 7.9 | 8.05 | 2 |
| 1 | 2024-07-10 | 7.9 | 8.0 | 3 |
| 1 | 2024-10-01 | 7.4 | 7.73 | 3 |
| 2 | 2024-02-11 | 9.1 | 9.1 | 1 |
| 2 | 2024-05-20 | 8.8 | 8.95 | 2 |
The frame ROWS BETWEEN 2 PRECEDING AND CURRENT ROW is the whole
trick: "this reading and the two before it." The hba1c column shows
exactly which values went into each average, and n_in_avg makes the
edge effect visible: the first reading averages just 1 value, the second averages
2 — only from the third row on is it a true 3-reading average. The frame simply
has fewer rows to work with early on; no error, but worth knowing when you read
the first few values.
SELECT patient_id, admin_date,
SUM(dose_mg) OVER (PARTITION BY patient_id ORDER BY admin_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS running_total_mg
FROM med_admin ORDER BY patient_id, admin_date;
| patient_id | admin_date | running_total_mg |
|---|---|---|
| 1 | 2024-01-05 | 500.0 |
| 1 | 2024-02-05 | 1000.0 |
| 1 | 2024-03-05 | 1250.0 |
| 2 | 2024-02-11 | 850.0 |
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW: "everything from the patient's first row
up to this one." Cumulative dose, cumulative cost, cumulative events — same shape.
ORDER BY inside OVER defines row order — without it, ROW_NUMBER() is arbitrary.ROWS counts physical rows; RANGE groups tied values. Say ROWS when you mean rows.PARTITION BY and your 'per-patient' running total silently runs across all patients.WHERE/GROUP BY — you can't put one in a WHERE clause; wrap it in a subquery or CTE first.