lag(value: storable, lagby: const int64) -> value

Return the value of one column in a previous row across an ordered group. offset is the number of rows backwards from the current row to obtain a value. If no ordering is specified, the default ordering is that of the invoking verb, which is generally the valid_from timestamp, ascending. In this case, lag(col, 1) returns the most recent prior value for column col.

Domain

This is a window function (calculates over a group of multiple input rows using windowing).

Categories

Examples

make_col prev_value:window(lag(value, 1), group_by(key))

Obtain the most recent value within the group identified by key and store the results in a new column named prev_value. Because we did not specify a order_by clause in the window function nor in the verb, the lag function would use the default ordering, which is time ascending.

timestampkeyvalueprev_value
100A5null
150B6null
180A45
300B56
400A34
make_col high_value:window(lag(value, 2), order_by(value, false), group_by(key))

Obtain the second highest value below the current value within the group identified by key and store the results in a new column named high_value. We use an order_by function to order the rows by value ascending.

timestampkeyvaluehigh_value
100A53
200B62
200A4null
300B5null
400A3null
400B2null
500A74
make_col prev_value:window(lag(value, 1), group_by(key), frame_exact(back:100ns))

Obtain the most recent value within the group identified by key and store the results in a new column named prev_value. Because a frame_exact is specified for the window function, prev_value would be null if the most recent value is found outside the window frame.

timestampkeyvalueprev_value
100A5null
150B6null
180A45
300B5null
400A3null