Skip to main content

next

Knowledge Checkpoint

Readings that will help you understand this documentation better:

Definition

A window function that returns the value of the next row at an offset relative to the current row.

Syntax

next(expr, offset, order: order_expr, ...)
next(expr, offset, order: order_expr, ...)
next(expr, offset, order: order_expr, ..., reset: partition_expr, ...)
next(expr, offset, order: order_expr, ..., partition: partition_expr, ...)
Examples
next(count(users.id), order: count(users.id) | desc())
next(count(users.id), 2, order: users.created_at | month())
next(count(users.id), 4, order: users.created_at | month(), reset: users.gender)

Input

  • expr (required): An expression of the value
  • offset (optional): The offset of the next row relative to the current row. If not specified, the default value is 1.
  • order (required, repeatable): A field that is used for ordering. The order is default to ascending. The order can be set explicitly with asc() or desc().
    danger

    If the specified order does not uniquely identify rows, the result of the function can be non-deterministic. For example, if you use order: users.age, and there are multiple users with the same age in the same partition, the result can be unexpected.

  • partition or reset (repeatable, optional): A field that is used for partitioning the table. If partitions are not specified:
    • If order is specified, the table will be partitioned by all other grouping columns.
    • If order is not specified, the table will be considered as a single partition.

Output

The value of the next row in a offset relative to the current row.

Sample Usages

Please refer to the sample usages in previous.


Let us know what you think about this document :)