在下面提供的解决方案(包括输入数据作为因式分解的子查询),第一我展示如何使用unpivot
和附加操作正常化tab1
(结果是因子分解子查询n
为“n
ormalized” )。然后,如果你有正常形式的数据,则可以通过直接应用我的代码底部显示的标准分层查询来获得输出结果。
with
tab1 (key, L1, L2, L3) as (
select 'A', 'A', null, null from dual union all
select 'B', 'A', 'B' , null from dual union all
select 'C', 'A', 'B' , 'C' from dual union all
select 'D', 'A', 'B' , 'D' from dual
),
tab2 (key, TC) as (
select 'A', 10 from dual union all
select 'B', 11 from dual union all
select 'C', 6 from dual union all
select 'D', 12 from dual union all
select 'X', 11 from dual
),
unpiv (key, l, ancestor) as (
select key, to_number(substr(lv, 2)), ancestor from tab1
unpivot (ancestor for lv in (L1, L2, L3))
),
d (key, depth) as (
select key, max(l)
from unpiv
group by key
),
n (child, parent, TC) as (
select d.key, u.ancestor, tab2.TC
from unpiv u
right outer join d
on u.key = d.key and u.l = d.depth - 1
left outer join tab2
on d.key = tab2.key
)
SELECT key, sum(TC) as sum_TC
from (
select connect_by_root child as key, TC
from n
connect by prior child = parent
)
group by key
order by key;
一路上,在unpiv
,我已经把所有的父子关系,所以我可以直接加入与tab2
上unpiv.key = tab2.key
和ancestor
(类似于MT0的解决方案)总结TC
分组。相反,我想演示两个单独的步骤:(1)规范化tab1
和(2)在规范化表上使用分层查询是多么容易。
输出:
KEY SUM_TC
--- ----------
A 39
B 29
C 6
D 12
这里是一个伟大的地方开始。 http://spaghettidba.com/2015/04/24/how-to-post-at-sql-question-on-a-public-forum/ –
请为此数据提供所需的输出,因为它不清楚什么你的意思是*每个级别的总和*。我想至少有3种解释。 – trincot
添加到trincot的评论 - 不要只提供所需的输出,用英文解释(无代码!)如何输出。 – mathguy