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

Availability

Available on Trino. Not available on DuckDB, Serving.

Examples

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