NTILE() OVER ...

All functions > WINDOW FUNCTION > NTILE() OVER ...

Returns the bucket number (1 to n) for each row in a partition, where n is the number of buckets.

Syntax

NTILE(expr) OVER ([PARTITION BY expr [, ...]] [ORDER BY sort_item [, ...]] [ROWS|RANGE|GROUPS frame])

Notes

  • Divides partition rows into n buckets numbered 1 to n
  • Requires ORDER BY to determine row ordering
  • PARTITION BY creates independent bucket groups
  • Distributes rows as evenly as possible across buckets
  • If rows don't divide evenly, earlier buckets get one extra row
  • Useful for percentile calculations and data sampling
  • Example: NTILE(4) creates quartiles, NTILE(100) creates percentiles
  • Always returns BIGINT type

See also

Examples

FeatureQL
SELECT
    f1 := ZIP(ARRAY[100, 110, 120] AS sort_key, ARRAY[10, 20, 30] AS v).TRANSFORM(SELECT NTILE(2) OVER (ORDER BY sort_key ASC)).UNWRAP() -- NTILE bucket labels (1..n), not sort_key (100, 110, 120)
;
Result
f1 ARRAY
[1, 1, 2]

Last update at: 2026/05/26 17:22:09