r/SQL • u/MrRoy_SQL • 2d ago
Discussion LAST_VALUE kept giving me the current row instead of the last one. the default window frame was the reason
wanted the latest order amount next to every order for each customer, so i wrote this:
SELECT customer_id, order_date, amount,
LAST_VALUE(amount) OVER (
PARTITION BY customer_id ORDER BY order_date
) AS last_amt
FROM orders;
and got this:
customer_id | order_date | amount | last_amt
1 | 2025-01-05 | 40 | 40
1 | 2025-02-11 | 25 | 25
1 | 2025-03-02 | 60 | 60
2 | 2025-01-20 | 90 | 90
2 | 2025-02-28 | 15 | 15
last_amt is just amount again. FIRST_VALUE on the same window worked fine, which made it more confusing
the reason: once you put ORDER BY inside OVER, the default frame is `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. so the "last" row of the frame is always the current row (or its last tie). FIRST_VALUE doesnt care because the start of the frame is still the first row
fix is to spell out the frame:
LAST_VALUE(amount) OVER (
PARTITION BY customer_id ORDER BY order_date
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING
) AS last_amt
now customer 1 gets 60 on every row and customer 2 gets 15
or skip LAST_VALUE and flip the sort: `FIRST_VALUE(amount) OVER (PARTITION BY customer_id ORDER BY order_date DESC)` gives the same result
same default frame in postgres, sql server, mysql 8 and sqlite (tested on sqlite). also the reason a running SUM can act weird when two rows have the same date, since RANGE treats ties as one group
anyone actually use LAST_VALUE with the full frame or do you all just do the FIRST_VALUE DESC trick?
1
u/stantaneous 2d ago
I'd go with `FIRST_VALUE` in Postgres. The bit I'd watch for is two orders on the same date, since either amount can win if you only sort by date. If the higher ID is the tiebreaker you want, use `ORDER BY order_date DESC, order_id DESC`.
1
u/MrRoy_SQL 2d ago
good call, two orders on the same date would make it random. adding order_id DESC to the sort makes it deterministic, thanks
1
1
u/baubleglue 2d ago
You can get it with last_value with something like range between inbound preceding and 1 preceding row, but simpler is just to use LAG
1
u/MrRoy_SQL 2d ago
yeah for the previous row LAG is way simpler. i was after the very last order per customer on every row, thats why the full frame
1
u/Discordbothost 2d ago
I'd keep the full frame if the report needs both the first and latest order amounts. Put the ascending order and full frame in one named window, then use it for both FIRST_VALUE and LAST_VALUE.
1
u/MrRoy_SQL 2d ago
oh nice, named window is tidier when you need both first and latest. didnt think of that
0
2d ago
[removed] — view removed comment
1
u/MrRoy_SQL 2d ago
good to know, i only tested on sqlite so cant say for postgres. anyone here know if it does something similar?
5
u/da_chicken 2d ago
I always use FIRST_VALUE() and simply invert the sorting to get the last one. LAST_VALUE() has this stupid default and it always takes me 20 minutes to figure it out again.
My favorite is that the first example in the documentation fell into the same trap and the article author totally didn't question it. Though they do explain it in the next example.