Loading...

vastorbit.sql.functions.lag

vastorbit.sql.functions.lag(expr: Annotated[str | list[str] | StringSQL | list[StringSQL], ''], offset: int = 1) StringSQL

Returns the value of the input expression at the given offset before the current row within a window.

Parameters:
  • expr (SQLExpression) – Expression.

  • offset (int) – Indicates how great is the lag.

Returns:

SQL string.

Return type:

StringSQL

Examples

First, let’s import the VastFrame in order to create a dummy dataset.

from vastorbit import VastFrame

Now, let’s import the vastorbit SQL functions.

import vastorbit.sql.functions as vof

We can now build a dummy dataset.

df = VastFrame(
    {
        "x": [1, 2, 3, 4],
        "y": [11.4, -2.5, 3.5, -4.2],
    },
)

Now, let’s go ahead and apply the function.

df["lag"] = vof.lag(df["y"], 1)._over(order_by = [df["x"]])
display(df)
123
x
Integer
100%
123
y
Decimal(3,1)
100%
123
lag
Decimal(3,1)
75%
1111.4[null]
22-2.511.4
333.5-2.5
44-4.23.5

Note

It’s crucial to utilize vastorbit SQL functions in coding, as they can be updated over time with new syntax. While SQL functions typically remain stable, they may vary across platforms or versions. vastorbit effectively manages these changes, a task not achievable with pure SQL.

See also

VastFrame.eval() : Evaluates the expression.