Loading...

verticapy.sql.functions.nth_value#

verticapy.sql.functions.nth_value(expr: 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#

StringSQL

SQL string.

Examples#

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

from verticapy import vDataFrame

Now, let’s import the VerticaPy SQL functions.

import verticapy.sql.functions as vpf

We can now build a dummy dataset.

df = vDataFrame(
    {
        "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"] = vpf.nth_value(df["y"], 3)._over(order_by = [df["x"]])
display(df)
123
x
Integer
100%
...
123
y
Numeric(5)
100%
123
nth_value
Numeric(5)
50%
11...11.4[null]
22...-2.5[null]
33...3.53.5
44...-4.23.5

Note

It’s crucial to utilize VerticaPy 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. VerticaPy effectively manages these changes, a task not achievable with pure SQL.

See also

vDataFrame.eval() : Evaluates the expression.