Loading...

vastorbit.sql.functions.nth_value

vastorbit.sql.functions.nth_value(expr: Annotated[str | list[str] | StringSQL | list[StringSQL], ''], row_number: int) StringSQL

Returns the value evaluated at the row that is the nth row of the window (counting from 1).

Parameters:
  • expr (SQLExpression) – Expression.

  • row_number (int) – Specifies the row to evaluate.

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["nth_value"] = vof.nth_value(df["y"], 3)._over(order_by = [df["x"]])
display(df)
123
x
Integer
100%
123
y
Decimal(3,1)
100%
123
nth_value
Decimal(3,1)
50%
1111.4[null]
22-2.5[null]
333.53.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.