LAG

functionsupportedCore SQL functions

docs.snowflake.com ↗

Window function syntax and usage

Accesses 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

NameRequiredDescription
exprrequiredThe expression to be returned based on the specified offset.
offsetrequiredThe 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.
defaultrequiredThe 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 } NULLSrequiredWhether 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

See also

Referenced by