I have a table `my_link_t`

with columns `weight t`

kind `number`

and categorical `speedcat`

write whole values from 2 to 8. I want to divide the data into groups of min = 0, max = 0.6, step = 0.001 and build a 3D graph to see the weight distribution for each category.

the initial data seems

speedcat weight

0.0234 2

0.8643 6

0.1854 7

(one hundred million more tickets with weight between 0 and 0.6 and speedcat between 2 and 8)

These queries return correct results and are completed in less than one minute each:

```
- repeat for each variable. Here we look for speedcat = 8
- It takes seconds to execute this query.
create histogram_tbl_8 table as
select ttt. "Start" as bucket_index, ttt.hist_row as bin8 --here
SINCE (
(
SELECT Bucket * 1 "Start", Bucket "End", Count (Ladle) hist_row
FROM (SELECT WIDTH_BUCKET (t.weight, 0, 0.599, 600) Bucket FROM my_link_t t where speedcat = 8)
GROUP BY CUBE ORDER BY Cube
) ttt
));
```

The previous query is repeated seven times for `speedcat`

in the range `2..8`

```
- If a container is empty, fill it with zero, do not omit it.
create histogram_output table as
select tr.bucket_index,
CASE
WHEN 1> (select the account (*) of histogram_tbl_2 htm where htm.bucket_index = tr.bucket_index) THEN 0
ELSE (select htm.bin2 from histogram_tbl_2 htm where htm.bucket_index = tr.bucket_index and rownum = 1)
FINISH
as b2,
- same for b3-b7
CASE
WHEN 1> (select the account (*) of histogram_tbl_8 htm where htm.bucket_index = tr.bucket_index) THEN 0
ELSE (select htm.bin8 from histogram_tbl_8 htm where htm.bucket_index = tr.bucket_index and rownum = 1)
FINISH
like b8
FROM (SELECT LEVEL as bucket_index, 0 as b2, / * 0 as b3, 0 as b4, 0 as b5, 0 as b6, 0 as b7, * / 0 as b8 FROM DUAL CONNECT BY LEVEL <600) tr
)
```

Finally

```
select sum (b2), sum (b3), sum (b4), sum (b5), sum (b6), sum (b7), sum (b8) of histogram_output
select bucket_index,
round (b2 * 1000000/12921) as b2, --normalize so that the total is 1000000 ppm
- repeat for b3-b7
round (b8 * 1000000/6262) as b8 --normalize so that the total is 1000000 ppm
from histogram_output
```

They give me a table like

bin_end speedcat_2 speedcat_3 speedcat_4 .. speedcat_8

0.001

0.002

..

0.599

0.600

Showing ppm of objects within this category and this container

Now, when I combine queries in

```
- DO NOT USE THE EXAMPLE BELOW - it is insufficient (runs more than 2 hours instead of seconds for the previous method)
SELECT Bucket_2 * 1 "Start", Bucket_2 "End",
Count (Bucket_2) as b2,
Count (Bucket_3) as b3,
Count (Bucket_4) as b4,
Count (Bucket_5) as b5,
Count (Bucket_6) as b6,
Count (Bucket_7) as b7,
Count (Bucket_8) as b8
--- 1 * round (ln (Count (Bucket) / (select count (*) from my_link_t)), 2) hist_row
SINCE
(
SELECT WIDTH_BUCKET (t2.weight, 0, 0.599, 600) Bucket_2,
WIDTH_BUCKET (t3.weight, 0, 0.599, 600) Bucket_3,
WIDTH_BUCKET (t4.weight, 0, 0.599, 600) Bucket_4,
WIDTH_BUCKET (t5.weight, 0, 0.599, 600) Bucket_5,
WIDTH_BUCKET (t6.weight, 0, 0.599, 600) Bucket_6,
WIDTH_BUCKET (t7.weight, 0, 0.599, 600) Bucket_7,
WIDTH_BUCKET (t8.weight, 0, 0.599, 600) Bucket_8
FROM (select the weight of my_link_t where speedcat = 2 and roadbase = & # 39; T & # 39;) t2,
(select the weight of my_link_t where speedcat = 3 and roadbase = & # 39; T & # 39;) t3,
(select the weight of my_link_t where speedcat = 4 and roadbase = & # 39; T & # 39;) t4,
(select the weight of my_link_t where speedcat = 5 and roadbase = & # 39; T & # 39;) t5,
(select the weight of my_link_t where speedcat = 6 and roadbase = & # 39; T & # 39;) t6,
(select the weight of my_link_t where speedcat = 7 and roadbase = & # 39; T & # 39;) t7,
(select the weight in my_link_t where speedcat = 8 and roadbase = & # 39; T & # 39;) t8
)
GROUP BY Bucket_2 ORDER BY Bucket_2
------
```

The query runs for several hours (about 500 times longer than individual queries) until I kill it. The books recommend doing the entire data division in SQL. This example suggests that loading data into Java and dividing it there could be better in case of complex queries.

What can cause the difference?