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
Examples
FeatureQL
SELECT
f1 := WIDTH_BUCKET(5E0, ARRAY[10E0, 20E0, 30E0, 40E0]), -- Below first bin
f2 := WIDTH_BUCKET(15E0, ARRAY[10E0, 20E0, 30E0, 40E0]), -- First bucket
f3 := WIDTH_BUCKET(25E0, ARRAY[10E0, 20E0, 30E0, 40E0]), -- Second bucket
f4 := WIDTH_BUCKET(45E0, ARRAY[10E0, 20E0, 30E0, 40E0]) -- Above last bin
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 0 | 1 | 2 | 4 |
On this page