我需要在分层查询中累积数据,如下所示:
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
除此之外,我只能访问前一行,因此它无法正常工作。而且我无法SELECT
对前一行使用当前子句中定义的别名:
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
我缺少什么?SYS_CONNECT_BY_PATH
完全按照我的需要进行操作(除了连接而不是乘法),所以应该是可以的。我可以使用WITH
子句来执行此操作,但出于某种原因,它非常慢,例如 需要半秒,CONNECT_BY
而 需要约 20 秒WITH
。
预期输出:
ID | 父代 ID | 数数 | COUNT_PRODUCT |
---|---|---|---|
1 | 无效的 | 1 | 1 |
2 | 1 | 2 | 2 |
3 | 2 | 3 | 6 |
4 | 3 | 4 | 24 |
5 | 4 | 5 | 120 |
6 | 5 | 6 | 720 |