DB2 Scalar functions - WIDTH_BUCKET
The WIDTH_BUCKET function is used to create equal-width histograms.
The schema is SYSIBM.
The data type of the result is based on the data type of num-buckets.
This function returns the bucket number that expression falls into given bound1, bound2, and num-buckets. The range from bound1 to bound2 is divided into num-buckets buckets starting from bucket 1 to bucket num-buckets.
If any argument can be null, the result can be null. If any argument is null, the result is the null value.
Using the EMPLOYEE table, assign a bucket to each employee's salary using a range of 35000 to 100000 divided into 13 buckets.
SELECT EMPNO , SALARY , WIDTH_BUCKET(SALARY, 35000, 100000, 13) FROM EMPLOYEE ORDER BY EMPNO
15 buckets are assigned with the following ranges:
The query has the following output:
EMPNO SALARY 3 ------ ----------- ----------- 000010 152750.00 14 000020 94250.00 12 000030 98250.00 13 000050 80175.00 10 000060 72250.00 8 000070 96170.00 13 000090 89750.00 11 000100 86150.00 11 000110 66500.00 7 000120 49250.00 3 000130 73800.00 8 000140 68420.00 7 000150 55280.00 5 000160 62250.00 6 000170 44680.00 2 000180 51340.00 4 000190 50450.00 4 000200 57740.00 5 000210 68270.00 7 000220 49840.00 3 000230 42180.00 2 000240 48760.00 3 000250 49180.00 3 000260 47250.00 3 000270 37380.00 1 000280 36250.00 1 000290 35340.00 1 000300 37750.00 1 000310 35900.00 1 000320 39950.00 1 000330 45370.00 3 000340 43840.00 2 200010 46500.00 3 200120 39250.00 1 200140 68420.00 7 200170 64680.00 6 200220 69840.00 7 200240 37760.00 1 200280 46250.00 3 200310 35900.00 1 200330 35370.00 1 200340 31840.00 0 42 record(s) selected.