Description
LAG returns the row that precedes the current row by offset number of rows. It is possible to specify a column based ordering and partitioning. Null values are skipped.
Syntax
LAG ( column [, ORDER BY ( sort_column [sorting], ... )] [, PARTITION BY ( partition_column, ... )] [, offset ] )
- column: The source column where preceding rows are taken from.
- sort_column: Optional sorting column to specify an order.
- sorting: Each of these columns can have an optional tag specifying the ordering of the column. Default is ascending:
- ASC: Ascending order
- DESC: Descending order
- partition_column: Optional partition column to specify groups in which
LAG
should operate. - offset: The number of non-NULL rows preceding the current row. The default value is 1.
Ordering
One or more columns can be given to specify an ordering. This tells the LAG
function what the preceding element
actually is. Optionally every column can be tagged as ascending or descending.
Partitioning
The partition columns specify groups. The LAG
function operates independently within every group. This means when
an ordering is given it is applied within every group and the last offset
elements in every group have
a lagging value of null.
LAG
with multiple partition columns.
Column1
LAG ( "Table1"."column" , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) )
LAG
with multiple partition columns.
Column1
LAG ( "Table1"."column" , ORDER BY ( "Table1"."year" ) , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) )
Result
Column1 : INT |
---|
2 |
null |
3 |
null |
null |
NULL handling
The lagging value for a NULL value is the same value as the lagging value for the next non-NULL value. The offset parameter counts only non-NULL values.
Examples
LAG
can be used to simulate ACTIVITY_LAG
.
Column1
LAG ( "Table1"."activity" , ORDER BY ( "Table1"."timestamp" ) , PARTITION BY ( "Table1"."case" ) )
Result
Column1 : STRING |
---|
null |
'A' |
'B' |
null |
'A' |
'B' |
'C' |
Column1
LAG ( "Table1"."column" , ORDER BY ( "Table1"."year" DESC , "Table1"."value" ) , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) )
Table1
column : INT | year : DATE | value : FLOAT | Country : STRING | State : STRING |
---|---|---|---|---|
1 | Tue Dec 31 2002 00:00:00.000 | 1.0 | 'Germany' | 'Bavaria' |
2 | Fri Dec 31 1999 00:00:00.000 | 2.0 | 'Germany' | 'Berlin' |
2 | Mon Dec 31 2001 00:00:00.000 | 3.0 | 'Germany' | 'Bavaria' |
3 | Fri Dec 31 1999 00:00:00.000 | 4.0 | 'Germany' | 'Bavaria' |
1 | Fri Dec 31 1999 00:00:00.000 | 5.0 | 'USA' | 'California' |
1 | Sun Dec 31 2000 00:00:00.000 | 6.0 | 'Germany' | 'Berlin' |
5 | Tue Dec 31 2002 00:00:00.000 | 7.0 | 'USA' | 'California' |
6 | Mon Dec 31 2001 00:00:00.000 | 8.0 | 'Germany' | 'Berlin' |
4 | Tue Dec 31 2002 00:00:00.000 | 9.0 | 'Germany' | 'Bavaria' |
7 | Wed Dec 31 2003 00:00:00.000 | 10.0 | 'USA' | 'California' |
Result
Column1 : INT |
---|
null |
1 |
4 |
2 |
5 |
6 |
7 |
null |
1 |
null |
Column1
LAG ( "Table1"."column" , ORDER BY ( "Table1"."year" DESC , "Table1"."value" ) , PARTITION BY ( "Table1"."Country" , "Table1"."State" ) , 2 )
Table1
column : INT | year : DATE | value : FLOAT | Country : STRING | State : STRING |
---|---|---|---|---|
1 | Tue Dec 31 2002 00:00:00.000 | 1.0 | 'Germany' | 'Bavaria' |
2 | Fri Dec 31 1999 00:00:00.000 | 2.0 | 'Germany' | 'Berlin' |
2 | Mon Dec 31 2001 00:00:00.000 | 3.0 | 'Germany' | 'Bavaria' |
3 | Fri Dec 31 1999 00:00:00.000 | 4.0 | 'Germany' | 'Bavaria' |
1 | Fri Dec 31 1999 00:00:00.000 | 5.0 | 'USA' | 'California' |
1 | Sun Dec 31 2000 00:00:00.000 | 6.0 | 'Germany' | 'Berlin' |
5 | Tue Dec 31 2002 00:00:00.000 | 7.0 | 'USA' | 'California' |
6 | Mon Dec 31 2001 00:00:00.000 | 8.0 | 'Germany' | 'Berlin' |
4 | Tue Dec 31 2002 00:00:00.000 | 9.0 | 'Germany' | 'Bavaria' |
7 | Wed Dec 31 2003 00:00:00.000 | 10.0 | 'USA' | 'California' |
Result
Column1 : INT |
---|
null |
6 |
1 |
4 |
7 |
null |
null |
null |
null |
null |