WIDTH_BUCKET_REGULAR
All functions > MATH > WIDTH_BUCKET_REGULAR
Returns the bucket number for a value in equally-spaced buckets within a range.
Syntax
WIDTH_BUCKET_REGULAR(expr, bound1, bound2, num)
sql
Arguments
Parameter | Type | Required | Description |
---|---|---|---|
value | DOUBLE | Yes | The value to categorize into a bucket |
min_value | DOUBLE | Yes | Minimum value of the range |
max_value | DOUBLE | Yes | Maximum value of the range |
num_buckets | BIGINT | Yes | Number of equal-width buckets to create |
Returns
BIGINT - Bucket number (1-based indexing)
Notes
- Creates num_buckets equal-width intervals between min_value and max_value
- Returns 0 if value < min_value
- Returns (num_buckets + 1) if value >= max_value
- Bucket width = (max_value - min_value) / num_buckets
- Useful for creating regular histograms
- Returns NULL if value is NULL