WIDTH_BUCKET

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Returns the bucket number to which an operand is assigned in a histogram of equal-width buckets.

Syntax 

1width_bucket(<operand>, <b1>, <b2>, <count> int)

Arguments 

Required 

  • <operand>: The value to assign to a bucket.
  • <b1>: The lower bound of the histogram range.
  • <b2>: The upper bound of the histogram range.
  • <count>: The number of equal-width buckets spanning the range b1 to b2.

Returns 

Returns the bucket number as an int. The lower bound b1 is included in the first bucket. Returns 0 for an input below the range, or count+1 for an input at or above the upper bound b2.

Considerations 

  • count must be greater than zero, and b1 and b2 can’t be equal. Otherwise, the function returns an error.
  • Reverse bounds (b1 greater than b2) to define a descending histogram.
  • Arguments can mix numeric types.

Examples 

Assign a Value to a Bucket 

Determine which histogram bucket a value falls into.

1SELECT width_bucket(5.35, 0.024, 10.06, 5) AS result;

Returns 3.

Value Below the Range 

An operand less than the lower bound returns 0.

1SELECT width_bucket(-1, 0.1, 1.1, 5) AS result;

Returns 0.

Value at the Upper Bound 

An operand at or above the upper bound returns count+1.

1SELECT width_bucket(1.1, 0.1, 1.1, 5) AS result;

Returns 6.

Related Documentation