Hi Tom,
thanks for your explanation. I like this feature!
I was just wondering if there is a (simple) way to make empty "buckets" visible. That would be of great help to display a histogram without distorting the x axis.
One example: I have an application where case numbers are created. However, as data is imported from different locations, not every number range is used: we have some "holes".
CREATE TABLE SAMPLE_TABLE (CASENUMBER number);
The table is populated from various sources.
SELECT WIDTH_BUCKET (CASENUMBER, 1, 5000000000, 500) * 10000000 - 10000000 "Bereich (von)",
WIDTH_BUCKET (CASENUMBER, 1, 5000000000, 500) * 10000000 - 1 "Bereich (bis)",
COUNT(*) Anzahl
FROM SAMPLE_TABLE
GROUP BY WIDTH_BUCKET (CASENUMBER, 1, 5000000000, 500)
ORDER BY 1 ASC;
My example shows:
Bereich (von) Bereich (bis) Anzahl
3050000000 3059999999 16903
3060000000 3069999999 38977
3070000000 3079999999 39091
3090000000 3099999999 1336
As we can see, there is a "hole" for the range between 3080000000 and 3089999999. I would like to display that as follows:
Bereich (von) Bereich (bis) Anzahl
3050000000 3059999999 16903
3060000000 3069999999 38977
3070000000 3079999999 39091
3080000000 3089999999 0
3090000000 3099999999 1336
Is there any way to do that?
Thanks!
July 31, 2017 - 3:01 am UTC
You just need to enumerate the possible buckets, and then outer join
SQL> with all_buckets as
2 ( select rownum-1 bk from dual
3 connect by level <= 12 ),
4 original_data as
5 (
6 select width_bucket(object_id,1,1000,10) w, count(*) hist
7 from t
8 group by width_bucket(object_id,1,1000,10)
9 )
10 select bk, nvl(hist,0) hist
11 from original_data, all_buckets
12 where bk = w(+)
13 order by 1;
BK HIST
---------- ----------
0 0
1 99
2 100
3 99
4 99
5 0
6 0
7 100
8 100
9 100
10 99
11 0
12 rows selected.