WIDTH_BUCKET()

All functions > MATH > WIDTH_BUCKET()

Returns the bucket number for a value in a histogram with custom bin boundaries.

Signatures

Custom bins

Returns: Bucket number (1-based)

WIDTH_BUCKET(value: T, bins: ARRAY(T)) → BIGINT
sql
ParameterTypeRequiredDescription
valueTYesValue to categorize into a bucket
binsARRAY(T)YesArray of boundary values in ascending order

With:

  • T : Floating-point type (FLOAT, DOUBLE)

Signature notes:

  • Returns 0 if value is less than the first boundary
  • Returns length(bins) + 1 if value is greater than the last boundary
  • Returns NULL if value is NULL

Examples

FeatureQL
SELECT
    f1 := WIDTH_BUCKET(5E0, ARRAY[10E0, 20E0, 30E0, 40E0]), -- Below first bin
    f2 := WIDTH_BUCKET(15E0, ARRAY[10E0, 20E0, 30E0, 40E0]), -- First bucket
    f3 := WIDTH_BUCKET(25E0, ARRAY[10E0, 20E0, 30E0, 40E0]), -- Second bucket
    f4 := WIDTH_BUCKET(45E0, ARRAY[10E0, 20E0, 30E0, 40E0]) -- Above last bin
;
Result
f1 BIGINTf2 BIGINTf3 BIGINTf4 BIGINT
0124

Last update at: 2026/05/26 17:22:09