width_bucket
function
Applies to: Databricks SQL Databricks Runtime
Returns the bucket number for a value in an equi-width histogram.
Arguments
expr
: An numeric or interval expression to be bucketed.minExpr
: A numeric or interval expression providing a lower bound for the buckets.maxExpr
: A numeric or interval expression providing an upper bound for the buckets.numBuckets
: An INTEGER expression greater than 0 specifying the number of buckets.
If expr
is numeric, minExpr
and maxExpr
must also be numeric.
If expr
is an interval, minExpr
and maxExpr
must be of the same interval type.
Interval is supported since: Databricks Runtime 10.1
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
.