Returns the value of an expression evaluated at the Nth row of the window frame. Returns NULL if the frame contains fewer than N rows.
For more information on usage, please refer to Window Functions.
Syntax
Parameters
| Parameter | Description | Supported input types |
|---|
<expression> | The value to evaluate and return at position N in the window frame. | Any |
<n> | The 1-based position within the window frame. Must be a positive integer. | INTEGER, BIGINT |
<partition_by> | An expression used for the PARTITION BY clause. | Any |
<order_by> | An expression used for the ORDER BY clause. | Any |
<frame_clause> | An optional window frame specification such as ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING. | — |
Return Type
Same as the input type of <expression>.
<n> must be greater than or equal to 1. Passing 0 or a negative value raises an error.
- Returns
NULL when N exceeds the number of rows in the window frame.
- This function respects
NULL values, and results will be ordered with default null ordering NULLS LAST unless otherwise specified in the ORDER BY clause.
Example
The following example returns the second-lowest maxpoints value within each leveltype, repeated for every row in that group:
| level int null | leveltype text null | maxpoints int null | second_lowest int null |
|---|
| 6 | Drift | 80 | 250 |
| 9 | Drift | 250 | 250 |
| 1 | FastestLap | 20 | 40 |
| 3 | FastestLap | 40 | 40 |
| 7 | FastestLap | 70 | 40 |
| 8 | FastestLap | 100 | 40 |
| 2 | FirstToComplete | 30 | 100 |
| 4 | FirstToComplete | 100 | 100 |
| 5 | FirstToComplete | 150 | 100 |
| 10 | FirstToComplete | 500 | 100 |
Rows: 10Execution time: 11.07ms