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

ParameterTypeRequiredDescription
valueDOUBLEYesThe value to categorize into a bucket
min_valueDOUBLEYesMinimum value of the range
max_valueDOUBLEYesMaximum value of the range
num_bucketsBIGINTYesNumber 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

Examples

Last update at: 2025/10/13 10:23:46