WIDTH_BUCKET
All functions > MATH > WIDTH_BUCKET
Returns the bucket number for a value in a histogram with custom bin boundaries.
Syntax
WIDTH_BUCKET(expr, bins)
sql
Arguments
Parameter | Type | Required | Description |
---|---|---|---|
value | DOUBLE | Yes | The value to categorize into a bucket |
bins_array | ARRAYDOUBLE | Yes | Array of boundary values defining the bins |
Returns
BIGINT - Bucket number (1-based indexing)
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