SQLite LAST_VALUE() Function
Example
Show each product with the most expensive product in its category:
SELECT CategoryID, ProductName, Price,
LAST_VALUE(ProductName) OVER (
PARTITION BY CategoryID
ORDER BY Price
ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING) AS MostExpensive
FROM Products;
Try it Yourself »
Definition and Usage
The LAST_VALUE() function returns the value from the last row in the window frame.
With ORDER BY, the default window frame ends at the current row, so LAST_VALUE() returns the value of the current row. To get the last row of the whole group, add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING.
Note: Window functions need an OVER clause. Read more in the SQLite Window Functions chapter.
Tip: Also look at FIRST_VALUE().
Syntax
LAST_VALUE(expression) OVER (ORDER BY column frame)
Parameter Values
| Parameter | Description |
|---|---|
| expression | Required. The column or calculation to return |
Technical Details
| Return type: | Same as the input |
|---|---|
| Works in: | SQLite 3.25.0 and later |