Skip to content

first_value ​

Returns the first value in the current ordered window frame. The function selects a row position; it retains a NULL at that position.

Frame bounds determine which rows can be selected. Use an explicit frame when the result should cover a fixed preceding range or the whole partition.

Syntax

text
first_value(value) OVER (
    [PARTITION BY expression, ...]
    ORDER BY expression, ...
    [ROWS | RANGE | GROUPS frame_bounds]
)

Arguments

  • value — The expression evaluated at the selected row.

Returned value The selected input value, preserving its native type. An empty frame returns NULL. Frame exclusions apply before selecting the position.

Examples Reading from a growing frame

The frame starts at the first sale and ends at the current row:

sql
CREATE TABLE sales (day INTEGER, amount INTEGER);
INSERT INTO sales VALUES (1, 120), (2, 150), (3, 90);
sql
SELECT day, amount,
       first_value(amount) OVER (
           ORDER BY day
           ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
       ) AS selected_amount
FROM sales
ORDER BY day
FORMAT csv;
text
1,120,120
2,150,120
3,90,120

Preserving NULL at the selected position

sql
CREATE TABLE readings (day INTEGER, value INTEGER);
INSERT INTO readings VALUES (1, NULL), (2, 20);
sql
SELECT day, first_value(value) OVER (ORDER BY day)
FROM readings
ORDER BY day
FORMAT csv;
text
1,\N
2,\N

See window functions for supported frame units and exclusions.