I need to accumulate data in hierarchical query like this:
select
id,
prior id as parent_id,
count * prior count --need to be accumulated through whole hierarchy like sys_connect_by_path
from table
start with id = 1
connect by prior id = parent_id
Except, i can only access prior row, so it will not work properly. And i cannot use alias defined in current SELECT clause for prior row:
select
id,
prior id as parent_id,
count,
(count * prior whole_count) as count_product --not allowed to do this :(
from table
start with id = 1
connect by prior id = parent_id
What i'm missing? SYS_CONNECT_BY_PATH doing exactly what i need (except concatenation instead of multiplication), so it should be possible.
I can do this using WITH clause, but for some reason it is extremely slow, like half a second for CONNECT_BY vs ~20 for WITH.
Expected output:
| ID | PARENT_ID | COUNT | COUNT_PRODUCT |
|---|---|---|---|
| 1 | null | 1 | 1 |
| 2 | 1 | 2 | 2 |
| 3 | 2 | 3 | 6 |
| 4 | 3 | 4 | 24 |
| 5 | 4 | 5 | 120 |
| 6 | 5 | 6 | 720 |
CREATE TABLEandINSERTstatements for your sample data; an English (not code or pseudo-code) explanation of the logic that you are trying to implement; and the expected output for your sample data. At the moment, it is difficult to understand whatcount * prior countis trying to achieve and whether it isCOUNT(*)or the product of successive "count" values over the hierarchy (in which case what is a "count")?