LAG
functionsupportedCore SQL functionsAccesses data in a previous row in the same result set without having to join the table to itself.
Syntax
supportedpartialnot supportednot scored
LAG ( <expr> [ , <offset> , <default> ] ) [ { IGNORE | RESPECT } NULLS ]
OVER ( [ PARTITION BY <expr1> ] ORDER BY <expr2> [ { ASC | DESC } ] [ NULLS { FIRST | LAST } ] )
Parameters
| Name | Required | Description |
|---|---|---|
expr | required | The expression to be returned based on the specified offset. |
offset | required | The number of rows backward from the current row from which to obtain a value. For example, an offset of 2 returns the expr value with an interval of 2 rows. Note that setting a negative offset has the same effect as using the LEAD function. Default is 1. |
default | required | The expression to return when the offset goes out of the bounds of the window. Supports any expression whose type is compatible with expr. Default is NULL. |
{ IGNORE | RESPECT } NULLS | required | Whether to ignore or respect NULL values when an expr contains NULL values: IGNORE NULLS excludes any row whose expression evaluates to NULL when offset rows are counted. RESPECT NULLS includes any row whose expression evaluates to NULL when offset rows are counted. Default: RESPECT NULLS |
Usage notes
- The PARTITION BY clause partitions the result set produced by the FROM clause into partitions to which the function is applied. For more information, see Window function syntax and usage.
- The ORDER BY clause orders the data within each partition.