WIDTH_BUCKET
All functions > MATH > WIDTH_BUCKET
Returns the bucket number for a value in a histogram with custom bin boundaries.
Signatures
Returns: Bucket number (1-based indexing)
WIDTH_BUCKET(value: T, bins_array: ARRAY(DOUBLE)) → BIGINT sql
| Parameter | Type | Required | Description |
|---|---|---|---|
value | T | Yes | The value to categorize into a bucket |
bins_array | ARRAY(DOUBLE) | Yes | Array of boundary values defining the bins |
Notes
- Returns bucket number based on where value falls in the bins
- Bins are defined by boundary values in ascending order
- Returns 0 if value is less than first boundary
- Returns (array_length + 1) if value is greater than last boundary
- Useful for creating histograms and data categorization
- Returns NULL if value is NULL
Examples
FeatureQL
SELECT
f1 := WIDTH_BUCKET(5e0, ARRAY[10e0, 20e0, 30e0, 40e0]), -- Value below first bin
f2 := WIDTH_BUCKET(15e0, ARRAY[10e0, 20e0, 30e0, 40e0]), -- Value in first bucket
f3 := WIDTH_BUCKET(25e0, ARRAY[10e0, 20e0, 30e0, 40e0]), -- Value in second bucket
f4 := WIDTH_BUCKET(45e0, ARRAY[10e0, 20e0, 30e0, 40e0]) -- Value above last bin
;Result
| f1 BIGINT | f2 BIGINT | f3 BIGINT | f4 BIGINT |
|---|---|---|---|
| 0 | 1 | 2 | 4 |
On this page