我需要像这样在分层查询中积累数据:
select
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,
count,
(count * prior whole_count) as whole_count --not allowed to do this :(
from table
start with id = 1
connect by prior id = parent_id
我缺少什么?
SYS_CONNECT_BY_PATH
正在做我需要的事情,所以这应该是可能的。
我可以使用 WITH
子句来做到这一点,但由于某种原因,它非常慢,比如 CONNECT_BY
需要半秒,而 WITH
需要大约 20 秒。
如果您计算层次结构中祖先的数量,那么这与查找层次结构的深度相同。为此,请使用
LEVEL
:
SELECT id,
LEVEL AS count
FROM table
START WITH id = 1
CONNECT BY PRIOR id = parent_id