根据条件在层次结构中识别停止汇总
Identify a Stop Roll Up within a Hierarchy Based on Conditions
我能够成功编写一个 returns 层次结构的查询,该层次结构有 7 层深。我想为每一行添加一个具有 "stop" 值的列。
请在此处查看示例:https://docs.oracle.com/cd/B19306_01/server.102/b14200/img/sqlrf002.gif
例如,参考上面的层次结构,我希望汇总到节点 2 的所有节点都具有 "stop value" 2。这需要根据某些条件进行设置。例如,如果节点 9 不符合标准,“向节点 9 报告的所有节点的停止位置将显示节点 1。标准只会在级别 2 完成。
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
我能够成功编写一个 returns 层次结构的查询,该层次结构有 7 层深。我想为每一行添加一个具有 "stop" 值的列。 请在此处查看示例:https://docs.oracle.com/cd/B19306_01/server.102/b14200/img/sqlrf002.gif
例如,参考上面的层次结构,我希望汇总到节点 2 的所有节点都具有 "stop value" 2。这需要根据某些条件进行设置。例如,如果节点 9 不符合标准,“向节点 9 报告的所有节点的停止位置将显示节点 1。标准只会在级别 2 完成。
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