我能够成功地编写一个查询,该查询返回一个层次结构,该层次结构深度为7级。我想为每行添加一个具有“停止”值的列。 请在此处查看示例:https://docs.oracle.com/cd/B19306_01/server.102/b14200/img/sqlrf002.gif
例如,参考上面的层次结构,我希望所有汇总到节点2的节点的“停止值”为2。这需要根据某些条件进行设置。例如,如果节点9不符合条件,则“向节点9报告的所有节点的停止位置将显示节点1。条件只能在级别2上完成。
答案 0 :(得分:0)
with criteria(guy, criterion) as (
select 2,1 from dual
)
, data(son, dad) as (
select 2,1 from dual union all
select 7,1 from dual union all
select 9,1 from dual union all
select 3,2 from dual union all
select 4,2 from dual union all
select 8,7 from dual union all
select 10,9 from dual union all
select 12,9 from dual union all
select 5,4 from dual union all
select 6,4 from dual union all
select 11,10 from dual
)
, hier as (
select connect_by_root(dad) level_1_guy,
connect_by_root(son) level_2_guy,
level lvl,
son
from data
start with dad = 1
connect by dad = prior son
)
select h.son,
case when c.criterion = 1
then h.level_2_guy
else h.level_1_guy
end stop_value
from hier h
left join criteria c on h.level_2_guy = c.guy
where lvl > 1;
SON STOP_VALUE
---------- ----------
3 2
4 2
5 2
6 2
8 1
10 1
11 1
12 1