29. Window frames: running totals, moving averages, lag and lead
Choose exactly which rows a window function sees from each row: everything so far, the last three, the one before.
A table of sales to work on
This chapter works on each order's total, again and again. To keep the examples short, we first save the totals of the orders that were not cancelled into a table of their own, sale, with CREATE TABLE ... AS SELECT (a new table made from a query's result). Chapter 30 shows a better way to keep a query: a view.
CREATE TABLE sale AS
SELECT o.id, o.customer_id, o.ordered, sum(oi.qty * oi.price) AS total
FROM orders AS o JOIN order_item AS oi ON oi.order_id = o.id
WHERE o.status <> 'cancelled'
GROUP BY o.id;
SELECT * FROM sale ORDER BY ordered;| id | customer_id | ordered | total |
|---|---|---|---|
| 1 | 1 | 2026-09-02 | 180.0 |
| 2 | 2 | 2026-09-05 | 165.0 |
| 3 | 1 | 2026-09-12 | 175.0 |
| 4 | 3 | 2026-09-20 | 300.0 |
| 6 | 5 | 2026-10-03 | 290.0 |
| 7 | 2 | 2026-10-10 | 235.0 |
| 8 | 6 | 2026-10-15 | 390.0 |
| 9 | 7 | 2026-10-22 | 490.0 |
| 10 | 1 | 2026-10-28 | 45.0 |
| 11 | 8 | 2026-11-02 | 300.0 |
| 12 | 4 | 2026-11-05 | 175.0 |
| 13 | 6 | 2026-11-11 | 90.0 |
| 15 | 7 | 2026-11-25 | 375.0 |
13 orders: orders 5 and 14 were cancelled, so they aren't there.
Running totals
A running total adds up everything from the first row to this one: the takings of the month so far, a bank balance after each transaction. With a window function it is just sum() with ORDER BY inside OVER:
SELECT id, ordered, total,
sum(total) OVER (ORDER BY ordered) AS running_total
FROM sale
ORDER BY ordered;| id | ordered | total | running_total |
|---|---|---|---|
| 1 | 2026-09-02 | 180.0 | 180.0 |
| 2 | 2026-09-05 | 165.0 | 345.0 |
| 3 | 2026-09-12 | 175.0 | 520.0 |
| 4 | 2026-09-20 | 300.0 | 820.0 |
| 6 | 2026-10-03 | 290.0 | 1110.0 |
| 7 | 2026-10-10 | 235.0 | 1345.0 |
| 8 | 2026-10-15 | 390.0 | 1735.0 |
| 9 | 2026-10-22 | 490.0 | 2225.0 |
| 10 | 2026-10-28 | 45.0 | 2270.0 |
| 11 | 2026-11-02 | 300.0 | 2570.0 |
| 12 | 2026-11-05 | 175.0 | 2745.0 |
| 13 | 2026-11-11 | 90.0 | 2835.0 |
| 15 | 2026-11-25 | 375.0 | 3210.0 |
Each row's running_total is its own total plus all the ones before it: 180, then 180 + 165 = 345, then 520, and so on up to 3210, the takings of all 13 orders.
Why does it add only up to this row, when in chapter 28 avg(price) OVER () saw every row? Because of the frame. A frame is the part of the window that the function sees from the current row. When OVER has an ORDER BY, the frame starts at the first row and ends at the current row, so sum() becomes a running sum. Without ORDER BY, the frame is the whole window.
With PARTITION BY, the running total starts again for each group. Here is how much each customer has spent so far, order by order:
SELECT customer_id, id, ordered, total,
sum(total) OVER (PARTITION BY customer_id ORDER BY ordered) AS customer_so_far
FROM sale
ORDER BY customer_id, ordered;| customer_id | id | ordered | total | customer_so_far |
|---|---|---|---|---|
| 1 | 1 | 2026-09-02 | 180.0 | 180.0 |
| 1 | 3 | 2026-09-12 | 175.0 | 355.0 |
| 1 | 10 | 2026-10-28 | 45.0 | 400.0 |
| 2 | 2 | 2026-09-05 | 165.0 | 165.0 |
| 2 | 7 | 2026-10-10 | 235.0 | 400.0 |
| 3 | 4 | 2026-09-20 | 300.0 | 300.0 |
| 4 | 12 | 2026-11-05 | 175.0 | 175.0 |
| 5 | 6 | 2026-10-03 | 290.0 | 290.0 |
| 6 | 8 | 2026-10-15 | 390.0 | 390.0 |
| 6 | 13 | 2026-11-11 | 90.0 | 480.0 |
| 7 | 9 | 2026-10-22 | 490.0 | 490.0 |
| 7 | 15 | 2026-11-25 | 375.0 | 865.0 |
| 8 | 11 | 2026-11-02 | 300.0 | 300.0 |
Customer 1 spent 180, then 355 after the second order, then 400; customer 7 has spent 865 in two orders.
Choosing the frame: ROWS BETWEEN
You can say exactly which rows the frame holds with ROWS BETWEEN start AND end. The start and end can be:
| Written | Means |
|---|---|
UNBOUNDED PRECEDING | the first row of the window |
n PRECEDING | n rows before this one |
CURRENT ROW | this row |
n FOLLOWING | n rows after this one |
UNBOUNDED FOLLOWING | the last row of the window |
A moving average of the last 3 orders evens out the ups and downs, to show where the takings are heading. The frame is "2 rows before, up to this row":
SELECT id, ordered, total,
count(*) OVER (ORDER BY ordered ROWS BETWEEN 2 PRECEDING AND CURRENT ROW) AS n,
round(avg(total) OVER (ORDER BY ordered ROWS BETWEEN 2 PRECEDING AND CURRENT ROW), 2) AS avg_last_3
FROM sale
ORDER BY ordered;| id | ordered | total | n | avg_last_3 |
|---|---|---|---|---|
| 1 | 2026-09-02 | 180.0 | 1 | 180.0 |
| 2 | 2026-09-05 | 165.0 | 2 | 172.5 |
| 3 | 2026-09-12 | 175.0 | 3 | 173.33 |
| 4 | 2026-09-20 | 300.0 | 3 | 213.33 |
| 6 | 2026-10-03 | 290.0 | 3 | 255.0 |
| 7 | 2026-10-10 | 235.0 | 3 | 275.0 |
| 8 | 2026-10-15 | 390.0 | 3 | 305.0 |
| 9 | 2026-10-22 | 490.0 | 3 | 371.67 |
| 10 | 2026-10-28 | 45.0 | 3 | 308.33 |
| 11 | 2026-11-02 | 300.0 | 3 | 278.33 |
| 12 | 2026-11-05 | 175.0 | 3 | 173.33 |
| 13 | 2026-11-11 | 90.0 | 3 | 188.33 |
| 15 | 2026-11-25 | 375.0 | 3 | 213.33 |
n shows how many rows were in the frame: the first order has only itself (1), the second has 2, and from the third on there are always 3. When order 10 comes in at only 45 baht, the average of the last three drops from 371.67 to 308.33.
Near the start, a frame holds fewer rows than you asked for, as the first two rows here show. A "3-order average" on the first row is really the average of one order. If that would mislead, show count(*) beside it as here, or leave those rows out.
The default frame and the last_value() trap
first_value(x) gives x from the first row of the frame, last_value(x) from its last row. Try to find the cheapest and the dearest product:
SELECT name, price,
first_value(name) OVER (ORDER BY price, name) AS cheapest,
last_value(name) OVER (ORDER BY price, name) AS dearest
FROM product
ORDER BY price, name;| name | price | cheapest | dearest |
|---|---|---|---|
| Pen | 15.0 | Pen | Pen |
| Highlighter | 25.0 | Pen | Highlighter |
| Potato Chips | 25.0 | Pen | Potato Chips |
| Notebook | 30.0 | Pen | Notebook |
| Croissant | 35.0 | Pen | Croissant |
| Orange Juice | 40.0 | Pen | Orange Juice |
| Green Tea | 45.0 | Pen | Green Tea |
| Coffee | 55.0 | Pen | Coffee |
| Banana Bread | 60.0 | Pen | Banana Bread |
| Dried Mango | 90.0 | Pen | Dried Mango |
| Cheesecake | 120.0 | Pen | Cheesecake |
| Cashew Nuts | 150.0 | Pen | Cashew Nuts |
cheapest is right, Pen on every row. But dearest is each row's own name! This is the trap beginners fall into most with windows: with ORDER BY in OVER, the default frame ends at the current row, so the last row of the frame is the current row itself. The fix is to say the frame should run to the end of the window:
SELECT name, price,
last_value(name) OVER (ORDER BY price, name
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS dearest
FROM product
ORDER BY price, name;| name | price | dearest |
|---|---|---|
| Pen | 15.0 | Cashew Nuts |
| Highlighter | 25.0 | Cashew Nuts |
| Potato Chips | 25.0 | Cashew Nuts |
| Notebook | 30.0 | Cashew Nuts |
| Croissant | 35.0 | Cashew Nuts |
| Orange Juice | 40.0 | Cashew Nuts |
| Green Tea | 45.0 | Cashew Nuts |
| Coffee | 55.0 | Cashew Nuts |
| Banana Bread | 60.0 | Cashew Nuts |
| Dried Mango | 90.0 | Cashew Nuts |
| Cheesecake | 120.0 | Cashew Nuts |
| Cashew Nuts | 150.0 | Cashew Nuts |
Now every row says Cashew Nuts.
nth_value(x, n) gives x from row n of the frame. With the same whole-window frame, it can find the second cheapest product of each category; first_value here needs no frame, because the first row is always in the default frame:
SELECT category_id, name, price,
first_value(name) OVER (PARTITION BY category_id ORDER BY price) AS cheapest,
nth_value(name, 2) OVER (PARTITION BY category_id ORDER BY price
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) AS second
FROM product
ORDER BY category_id, price;| category_id | name | price | cheapest | second |
|---|---|---|---|---|
| 1 | Orange Juice | 40.0 | Orange Juice | Green Tea |
| 1 | Green Tea | 45.0 | Orange Juice | Green Tea |
| 1 | Coffee | 55.0 | Orange Juice | Green Tea |
| 2 | Croissant | 35.0 | Croissant | Banana Bread |
| 2 | Banana Bread | 60.0 | Croissant | Banana Bread |
| 2 | Cheesecake | 120.0 | Croissant | Banana Bread |
| 3 | Potato Chips | 25.0 | Potato Chips | Dried Mango |
| 3 | Dried Mango | 90.0 | Potato Chips | Dried Mango |
| 3 | Cashew Nuts | 150.0 | Potato Chips | Dried Mango |
| 4 | Pen | 15.0 | Pen | Highlighter |
| 4 | Highlighter | 25.0 | Pen | Highlighter |
| 4 | Notebook | 30.0 | Pen | Highlighter |
lag() and lead(): the row before, the row after
lag(x) gives x from the row before this one, lead(x) from the row after. They don't use the frame; they just step back or forward in the window's order. That makes "how much has it changed since last time?" easy:
SELECT id, ordered, total,
lag(total) OVER (ORDER BY ordered) AS previous,
total - lag(total) OVER (ORDER BY ordered) AS change,
lead(ordered) OVER (ORDER BY ordered) AS next_order
FROM sale
ORDER BY ordered;| id | ordered | total | previous | change | next_order |
|---|---|---|---|---|---|
| 1 | 2026-09-02 | 180.0 | NULL | NULL | 2026-09-05 |
| 2 | 2026-09-05 | 165.0 | 180.0 | -15.0 | 2026-09-12 |
| 3 | 2026-09-12 | 175.0 | 165.0 | 10.0 | 2026-09-20 |
| 4 | 2026-09-20 | 300.0 | 175.0 | 125.0 | 2026-10-03 |
| 6 | 2026-10-03 | 290.0 | 300.0 | -10.0 | 2026-10-10 |
| 7 | 2026-10-10 | 235.0 | 290.0 | -55.0 | 2026-10-15 |
| 8 | 2026-10-15 | 390.0 | 235.0 | 155.0 | 2026-10-22 |
| 9 | 2026-10-22 | 490.0 | 390.0 | 100.0 | 2026-10-28 |
| 10 | 2026-10-28 | 45.0 | 490.0 | -445.0 | 2026-11-02 |
| 11 | 2026-11-02 | 300.0 | 45.0 | 255.0 | 2026-11-05 |
| 12 | 2026-11-05 | 175.0 | 300.0 | -125.0 | 2026-11-11 |
| 13 | 2026-11-11 | 90.0 | 175.0 | -85.0 | 2026-11-25 |
| 15 | 2026-11-25 | 375.0 | 90.0 | 285.0 | NULL |
The first row has no row before it, so previous is NULL, and change is NULL too (anything with NULL is NULL). The biggest fall is order 10, 445 baht less than order 9. The last row has no next_order.
lag takes two more arguments: how many rows back (1 if you leave it out) and what to give when there is no such row instead of NULL. With PARTITION BY it looks back only within the group, so it can tell how many days each customer waited between orders:
SELECT customer_id, id, ordered,
lag(ordered) OVER (PARTITION BY customer_id ORDER BY ordered) AS before,
julianday(ordered) - julianday(lag(ordered) OVER (PARTITION BY customer_id ORDER BY ordered)) AS days_between,
lag(total, 1, 0) OVER (PARTITION BY customer_id ORDER BY ordered) AS before_total
FROM sale
ORDER BY customer_id, ordered;| customer_id | id | ordered | before | days_between | before_total |
|---|---|---|---|---|---|
| 1 | 1 | 2026-09-02 | NULL | NULL | 0 |
| 1 | 3 | 2026-09-12 | 2026-09-02 | 10.0 | 180.0 |
| 1 | 10 | 2026-10-28 | 2026-09-12 | 46.0 | 175.0 |
| 2 | 2 | 2026-09-05 | NULL | NULL | 0 |
| 2 | 7 | 2026-10-10 | 2026-09-05 | 35.0 | 165.0 |
| 3 | 4 | 2026-09-20 | NULL | NULL | 0 |
| 4 | 12 | 2026-11-05 | NULL | NULL | 0 |
| 5 | 6 | 2026-10-03 | NULL | NULL | 0 |
| 6 | 8 | 2026-10-15 | NULL | NULL | 0 |
| 6 | 13 | 2026-11-11 | 2026-10-15 | 27.0 | 390.0 |
| 7 | 9 | 2026-10-22 | NULL | NULL | 0 |
| 7 | 15 | 2026-11-25 | 2026-10-22 | 34.0 | 490.0 |
| 8 | 11 | 2026-11-02 | NULL | NULL | 0 |
Customer 1 came back after 10 days, then after 46. Each customer's first order has no before, and before_total shows 0, the value we asked for, instead of NULL.
The WINDOW clause, RANGE and GROUPS
When several functions use the same window, writing it out again and again is long and easy to get wrong. Name it once in a WINDOW clause (after HAVING, before ORDER BY) and use the name after OVER:
SELECT id, ordered, total,
count(*) OVER last3 AS n,
sum(total) OVER last3 AS sum_last_3,
max(total) OVER last3 AS max_last_3
FROM sale
WINDOW last3 AS (ORDER BY ordered ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)
ORDER BY ordered;| id | ordered | total | n | sum_last_3 | max_last_3 |
|---|---|---|---|---|---|
| 1 | 2026-09-02 | 180.0 | 1 | 180.0 | 180.0 |
| 2 | 2026-09-05 | 165.0 | 2 | 345.0 | 180.0 |
| 3 | 2026-09-12 | 175.0 | 3 | 520.0 | 180.0 |
| 4 | 2026-09-20 | 300.0 | 3 | 640.0 | 300.0 |
| 6 | 2026-10-03 | 290.0 | 3 | 765.0 | 300.0 |
| 7 | 2026-10-10 | 235.0 | 3 | 825.0 | 300.0 |
| 8 | 2026-10-15 | 390.0 | 3 | 915.0 | 390.0 |
| 9 | 2026-10-22 | 490.0 | 3 | 1115.0 | 490.0 |
| 10 | 2026-10-28 | 45.0 | 3 | 925.0 | 490.0 |
| 11 | 2026-11-02 | 300.0 | 3 | 835.0 | 490.0 |
| 12 | 2026-11-05 | 175.0 | 3 | 520.0 | 300.0 |
| 13 | 2026-11-11 | 90.0 | 3 | 565.0 | 300.0 |
| 15 | 2026-11-25 | 375.0 | 3 | 640.0 | 375.0 |
ROWS counts rows. There are two other ways to measure a frame:
RANGEmeasures by the value of theORDER BY.RANGE BETWEEN 6 PRECEDING AND CURRENT ROWmeans "rows whose value is at most 6 less than this row's". Ordered by the day numberjulianday(ordered), that is "the 7 days up to and including today", however many orders those days hold.GROUPScounts groups of rows with the sameORDER BYvalue (peers) instead of single rows.
The orders and takings of the 7 days up to each order:
SELECT id, ordered, total,
count(*) OVER week AS orders_in_7_days,
sum(total) OVER week AS total_in_7_days
FROM sale
WINDOW week AS (ORDER BY julianday(ordered) RANGE BETWEEN 6 PRECEDING AND CURRENT ROW)
ORDER BY ordered;| id | ordered | total | orders_in_7_days | total_in_7_days |
|---|---|---|---|---|
| 1 | 2026-09-02 | 180.0 | 1 | 180.0 |
| 2 | 2026-09-05 | 165.0 | 2 | 345.0 |
| 3 | 2026-09-12 | 175.0 | 1 | 175.0 |
| 4 | 2026-09-20 | 300.0 | 1 | 300.0 |
| 6 | 2026-10-03 | 290.0 | 1 | 290.0 |
| 7 | 2026-10-10 | 235.0 | 1 | 235.0 |
| 8 | 2026-10-15 | 390.0 | 2 | 625.0 |
| 9 | 2026-10-22 | 490.0 | 1 | 490.0 |
| 10 | 2026-10-28 | 45.0 | 2 | 535.0 |
| 11 | 2026-11-02 | 300.0 | 2 | 345.0 |
| 12 | 2026-11-05 | 175.0 | 2 | 475.0 |
| 13 | 2026-11-11 | 90.0 | 2 | 265.0 |
| 15 | 2026-11-25 | 375.0 | 1 | 375.0 |
Order 8 (15 October) sees order 7 from 5 days before, so 2 orders, 625 baht. Order 3 (12 September) is exactly 7 days after order 2, just outside the frame, so it sees only itself.
The difference shows when the ORDER BY has ties. Highlighter and Potato Chips both cost 25:
SELECT name, price,
sum(price) OVER (ORDER BY price, id ROWS UNBOUNDED PRECEDING) AS rows_sum,
sum(price) OVER (ORDER BY price RANGE UNBOUNDED PRECEDING) AS range_sum,
count(*) OVER (ORDER BY price GROUPS BETWEEN 1 PRECEDING AND 1 FOLLOWING) AS near_prices
FROM product
ORDER BY price, id;| name | price | rows_sum | range_sum | near_prices |
|---|---|---|---|---|
| Pen | 15.0 | 15.0 | 15.0 | 3 |
| Potato Chips | 25.0 | 40.0 | 65.0 | 4 |
| Highlighter | 25.0 | 65.0 | 65.0 | 4 |
| Notebook | 30.0 | 95.0 | 95.0 | 4 |
| Croissant | 35.0 | 130.0 | 130.0 | 3 |
| Orange Juice | 40.0 | 170.0 | 170.0 | 3 |
| Green Tea | 45.0 | 215.0 | 215.0 | 3 |
| Coffee | 55.0 | 270.0 | 270.0 | 3 |
| Banana Bread | 60.0 | 330.0 | 330.0 | 3 |
| Dried Mango | 90.0 | 420.0 | 420.0 | 3 |
| Cheesecake | 120.0 | 540.0 | 540.0 | 3 |
| Cashew Nuts | 150.0 | 690.0 | 690.0 | 2 |
(ROWS UNBOUNDED PRECEDING is short for "from the first row to this one".) With ROWS, Potato Chips' running sum is 40 and Highlighter's is 65, one row at a time. With RANGE, both get 65: rows of the same price are peers, and a peer is always in the frame with the current row. This is also the default frame: when you write only ORDER BY, you get RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW. near_prices counts the products in this price group and the price groups either side: Pen sees itself and the two at 25, 3 in all.
A running total over a column with ties (say, several orders on the same day) gives the tied rows the same total, because of the default RANGE. If you want it to grow row by row, write ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW and add something that can't tie, such as id, to the ORDER BY.
- Show each customer in the order they joined, with how many members the shop had after each one joined.
- For each order in
sale, show the total of the same customer's previous order. - For each order in
sale, add up its total and the total of the order just before it.
Show the answer
SELECT name, joined, count(*) OVER (ORDER BY joined) AS members_so_far
FROM customer
ORDER BY joined;
SELECT customer_id, id, ordered, total,
lag(total) OVER (PARTITION BY customer_id ORDER BY ordered) AS previous_total
FROM sale
ORDER BY customer_id, ordered;
SELECT id, ordered, total,
sum(total) OVER (ORDER BY ordered ROWS BETWEEN 1 PRECEDING AND CURRENT ROW) AS last_two
FROM sale
ORDER BY ordered;| name | joined | members_so_far |
|---|---|---|
| Somchai Jaidee | 2025-01-15 | 1 |
| Malee Srisuk | 2025-03-02 | 2 |
| Anan Wongsa | 2025-04-20 | 3 |
| Pim Chaiya | 2025-06-11 | 4 |
| Narin Thongdee | 2025-08-30 | 5 |
| Kanya Boonmee | 2025-10-05 | 6 |
| Wichai Saelim | 2026-01-09 | 7 |
| Siriporn Kaewdee | 2026-02-14 | 8 |
| Tawan Rungruang | 2026-05-21 | 9 |
| Lalita Meesuk | 2026-08-03 | 10 |
| customer_id | id | ordered | total | previous_total |
|---|---|---|---|---|
| 1 | 1 | 2026-09-02 | 180.0 | NULL |
| 1 | 3 | 2026-09-12 | 175.0 | 180.0 |
| 1 | 10 | 2026-10-28 | 45.0 | 175.0 |
| 2 | 2 | 2026-09-05 | 165.0 | NULL |
| 2 | 7 | 2026-10-10 | 235.0 | 165.0 |
| 3 | 4 | 2026-09-20 | 300.0 | NULL |
| 4 | 12 | 2026-11-05 | 175.0 | NULL |
| 5 | 6 | 2026-10-03 | 290.0 | NULL |
| 6 | 8 | 2026-10-15 | 390.0 | NULL |
| 6 | 13 | 2026-11-11 | 90.0 | 390.0 |
| 7 | 9 | 2026-10-22 | 490.0 | NULL |
| 7 | 15 | 2026-11-25 | 375.0 | 490.0 |
| 8 | 11 | 2026-11-02 | 300.0 | NULL |
| id | ordered | total | last_two |
|---|---|---|---|
| 1 | 2026-09-02 | 180.0 | 180.0 |
| 2 | 2026-09-05 | 165.0 | 345.0 |
| 3 | 2026-09-12 | 175.0 | 340.0 |
| 4 | 2026-09-20 | 300.0 | 475.0 |
| 6 | 2026-10-03 | 290.0 | 590.0 |
| 7 | 2026-10-10 | 235.0 | 525.0 |
| 8 | 2026-10-15 | 390.0 | 625.0 |
| 9 | 2026-10-22 | 490.0 | 880.0 |
| 10 | 2026-10-28 | 45.0 | 535.0 |
| 11 | 2026-11-02 | 300.0 | 345.0 |
| 12 | 2026-11-05 | 175.0 | 475.0 |
| 13 | 2026-11-11 | 90.0 | 265.0 |
| 15 | 2026-11-25 | 375.0 | 465.0 |
Phai (bamboo), given as a gift of knowledge: free to use, learn from and pass on · every example on this page was run on Phai SQL and checked against SQLite when the page was made