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

ParameterTypeRequiredDescription
valueDOUBLEYesThe value to categorize into a bucket
bins_arrayARRAYDOUBLEYesArray 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

Examples

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