Skip to main content

ntile

Knowledge Checkpoint

Readings that will help you understand this documentation better:

Definition

A window function that divides the rows within a partition into a specified number of ranked groups. It assigns a rank (bucket number) to each row, based on the ordering specified.

Syntax

ntile(ranks, order: order_expr)
ntile(ranks, order: order_expr, ..., partition: partition_expr, ...)
Examples
ntile(4, order: count(orders.id) | desc())
ntile(4, order: count(orders.id) | desc(), order: average(users.age))

// with partition
ntile(4, order: count(orders.id) | desc(), partition: orders.status)

Input

  • ranks (required, number): The number of ranked groups to divide the rows into.
  • 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(). E.g. ntile(4, order: count(orders.id) | desc())
  • partition (repeatable, optional): A field that is used for partitioning the table. E.g. ntile(4, order: count(orders.id), partition: orders.status)

Output

The rank group (or bucket number) the current row belongs to, between 1 and ranks. If no partition is specified, the whole table is considered as a single partition.


Let us know what you think about this document :)