LAST_VALUE() OVER ...
All functions > WINDOW FUNCTION > LAST_VALUE() OVER ...
Returns the last value in the window frame.
Syntax
LAST_VALUE(expr) [WITHIN (WHERE condition)] OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])
Notes
- Returns the value from the last row of the window frame as defined by ORDER BY and the frame
- With the default frame ending at the current row, LAST_VALUE is usually the current row’s value, not the last row of the whole partition
- Use
ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING(or another explicit frame) when you need the partition’s true last value - Returns same type as input expression
- NULL values are preserved
See also
Examples
FeatureQL
SELECT
-- Default frame ends at current row, so LAST_VALUE is current-row v (V is permuted so this is not ascending order)
f1 := ZIP(ARRAY(1, 2, 3) AS id, ARRAY(30, 10, 20) AS v).TRANSFORM(
SELECT LAST_VALUE(v) OVER (ORDER BY id ASC)
).UNWRAP(),
-- Last matching row in the default frame; rows outside WITHIN return NULL
f2 := ZIP(
ARRAY(1, 2, 3, 4) AS id,
ARRAY(10, 20, 30, 40) AS v,
ARRAY(TRUE, FALSE, TRUE, TRUE) AS keep
).TRANSFORM(SELECT LAST_VALUE(v) WITHIN (WHERE keep) OVER (ORDER BY id ASC)).UNWRAP()
;Result
| f1 ARRAY | f2 ARRAY |
|---|---|
| [30, 10, 20] | [10, null, 30, 40] |