BigQuery · Window function
BigQuery NTILE
NTILE(n) splits the ordered rows of each window partition into n buckets of nearly equal size and returns the bucket number, from 1 to n. Use it for quartiles, deciles and equal-size customer segments; bucket sizes differ by at most one row.
NTILE sorts the partition by the window's ORDER BY and deals the rows into n buckets by position. Every bucket gets the same number of rows, and when the rows don't divide evenly the first buckets get one extra row each: 10 rows in 4 buckets come out as 3, 3, 2, 2. The bucket depends on position, not value, so rows with equal values can land in different buckets.
The argument must be an INT64 literal or query parameter of at least 1; a column or 0 is an error. ORDER BY is required. NULLs sort first in ascending order, so they fill the lowest buckets unless you filter them out or add NULLS LAST.
On large tables, NTILE without PARTITION BY needs a global sort on a single worker and can fail with Resources exceeded. Google's performance guide recommends computing bucket boundaries with APPROX_QUANTILES instead and assigning rows by comparing values with those boundaries.
Syntax
NTILE(constant_integer_expression) OVER ([PARTITION BY partition_expression [, ...]] ORDER BY expression [{ASC | DESC}] [, ...])Arguments and return value
| Argument | Description |
|---|---|
constant_integer_expression | Number of buckets. An INT64 literal or query parameter, at least 1. |
PARTITION BY partition_expression | Buckets each group separately. Optional. |
ORDER BY expression | Required. Decides which rows go into the first bucket. |
Returns: INT64 from 1 to n.
Examples
Spend quartiles
WITH customers AS (
SELECT customer_id, spend
FROM UNNEST([
STRUCT('c01' AS customer_id, 120 AS spend), ('c02', 950), ('c03', 40), ('c04', 310),
('c05', 75), ('c06', 640), ('c07', 220), ('c08', 15)
])
)
SELECT
customer_id,
spend,
NTILE(4) OVER (ORDER BY spend DESC) AS spend_quartile
FROM customers
ORDER BY spend DESC;| customer_id | spend | spend_quartile |
|---|---|---|
| c02 | 950 | 1 |
| c06 | 640 | 1 |
| c04 | 310 | 2 |
| c07 | 220 | 2 |
| c01 | 120 | 3 |
| c05 | 75 | 3 |
| c03 | 40 | 4 |
| c08 | 15 | 4 |
Uneven buckets
Ten rows don't divide into four buckets, so buckets 1 and 2 get one extra row each.
SELECT
bucket,
COUNT(*) AS rows_in_bucket,
MIN(x) AS min_x,
MAX(x) AS max_x
FROM (
SELECT x, NTILE(4) OVER (ORDER BY x) AS bucket
FROM UNNEST(GENERATE_ARRAY(1, 10)) AS x
)
GROUP BY bucket
ORDER BY bucket;| bucket | rows_in_bucket | min_x | max_x |
|---|---|---|---|
| 1 | 3 | 1 | 3 |
| 2 | 3 | 4 | 6 |
| 3 | 2 | 7 | 8 |
| 4 | 2 | 9 | 10 |
Equal values in different buckets
All four amounts are equal, yet NTILE puts two rows in each half.
SELECT
amount,
NTILE(2) OVER (ORDER BY amount) AS half
FROM UNNEST([50, 50, 50, 50]) AS amount
ORDER BY half;| amount | half |
|---|---|
| 50 | 1 |
| 50 | 1 |
| 50 | 2 |
| 50 | 2 |
Common mistakes
- Expecting value-based buckets. NTILE makes buckets with equal row counts, so equal values can be split and value ranges are uneven. For fixed value ranges use RANGE_BUCKET or CASE.
- Passing a column or expression as the bucket count. It fails with
The argument to NTILE must be a literal or query parameter. - Letting NULLs fill the first bucket. In ascending order NULLs come first; filter them or use NULLS LAST.
- Running NTILE over a whole large table. Without PARTITION BY it sorts every row on one worker; use APPROX_QUANTILES boundaries instead.
Cost and performance
- Google's performance guide recommends APPROX_QUANTILES over NTILE for large tables: an unpartitioned NTILE requires a global ORDER BY processed by a single worker, while APPROX_QUANTILES aggregates in parallel. The two place remainder rows differently, so bucket counts can differ slightly.
FAQ
How do I split rows into quartiles or deciles in BigQuery?
Use NTILE(4) OVER (ORDER BY metric) for quartiles and NTILE(10) for deciles. Add PARTITION BY to bucket each group separately.
What is the difference between NTILE and APPROX_QUANTILES?
NTILE labels every row with a bucket number and needs a sort of the whole partition. APPROX_QUANTILES returns the boundary values of a group as an array, scales to large tables, and leaves the comparison of each row with the boundaries to you.
Why are my NTILE buckets different sizes?
The row count isn't divisible by the number of buckets. The remainder goes to the first buckets, one extra row each, so sizes differ by at most one.
Related
Errors you may hit
More window functions
All BigQuery window functions · All functions · BigQuery error messages
