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
ParameterTypeRequiredDescription
valueTYesThe value to categorize into a bucket
bins_arrayARRAY(DOUBLE)YesArray 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 BIGINTf2 BIGINTf3 BIGINTf4 BIGINT
0124

Last update at: 2026/03/03 16:47:38
Last updated: 2026-03-03 16:48:19