WIDTH_BUCKET()
All functions > MATH > WIDTH_BUCKET()
Returns the bucket number for a value in a histogram with custom bin boundaries.
Signatures
Custom bins
Returns: Bucket number (1-based)
WIDTH_BUCKET(value: T, bins: ARRAY(T)) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
value | T | Yes | Value to categorize into a bucket |
bins | ARRAY(T) | Yes | Array of boundary values in ascending order |
With:
T: Floating-point type (FLOAT, DOUBLE)
Signature notes:
- Returns 0 if value is less than the first boundary
- Returns
length(bins) + 1if value is greater than the last boundary - Returns NULL if value is NULL
Availability
Available on Trino. Not available on DuckDB, Serving.
Examples
FeatureQL
SELECT
-- Below first bin
f1 := WIDTH_BUCKET(5e0, ARRAY(10e0, 20e0, 30e0, 40e0)),
-- First bucket
f2 := WIDTH_BUCKET(15e0, ARRAY(10e0, 20e0, 30e0, 40e0)),
-- Second bucket
f3 := WIDTH_BUCKET(25e0, ARRAY(10e0, 20e0, 30e0, 40e0)),
-- Above last bin
f4 := WIDTH_BUCKET(45e0, ARRAY(10e0, 20e0, 30e0, 40e0))
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 0 | 1 | 2 | 4 |
On this page