width_bucket function

Returns the bucket number for a value in an equi-width histogram.

Syntax

width_bucket(expr, minExpr, maxExpr, numBuckets)

Arguments

  • expr: An numeric expression to be bucketed.

  • minExpr: A numeric expression providing a lower bound for the buckets.

  • maxExpr: A numeric expression providing an upper bound for the buckets.

  • numBuckets: An INTEGER expression greater than 0 specifying the number of buckets.

Returns

An INTEGER.

The function divides the range between minExpr and maxExpr into numBuckets slices of equal size. The result is the slice into which expr falls.

If expr is outside of minExpr the result is 0.

If expr is outside of maxExpr the result is numbuckets + 1

minExpr can be greater than maxExpr.

Examples

> SELECT width_bucket(5.3, 0.2, 10.6, 5);
 3
> SELECT width_bucket(-2.1, 1.3, 3.4, 3);
 0
> SELECT width_bucket(8.1, 0.0, 5.7, 4);
 5
> SELECT width_bucket(-0.9, 5.2, 0.5, 2);
 3