다음은 SQL을 작성해야하는 데이터 설정입니다. SQL의 복잡한 계층 적 쿼리
Table:parchil
par chil
--------------------
E1 E2
E2 E3
E3 E4
E5 E6
E7 E8
Table:subval
sub val
--------------------
E1 10
E2 70
E3 30
E4 40
E5 60
E6 20
E7 50
Expected result:
sub val
--------------------
E1 150
E2 140
E3 70
E4 40
E5 80
E6 20
E7 50
나는 길고 우아한에서 멀리 지금까지 아래 쿼리가 있습니다.
select a.par,sum(b.val) from
(select 'E1' as par,'E1' as chil from dual
union all
select
'E1' as par, chil
from
parchil
start with par='E1'
connect by prior chil=par
union all
select 'E2' as par,'E2' as chil from dual
union all
select
'E2' as par, chil
from
parchil
start with par='E2'
connect by prior chil=par
union all
select 'E3' as par,'E3' as chil from dual
union all
select
'E3' as par, chil
from
parchil
start with par='E3'
connect by prior chil=par
union all
select 'E4' as par,'E4' as chil from dual
union all
select
'E4' as par, chil
from
parchil
start with par='E4'
connect by prior chil=par
union all
select 'E5' as par,'E5' as chil from dual
union all
select
'E5' as par, chil
from
parchil
start with par='E5'
connect by prior chil=par
union all
select 'E6' as par,'E6' as chil from dual
union all
select
'E6' as par, chil
from
parchil
start with par='E6'
connect by prior chil=par
union all
select 'E7' as par,'E7' as chil from dual
union all
select
'E7' as par, chil
from
parchil
start with par='E7'
connect by prior chil=par
) a,
subval b
where
a.chil=b.sub
group by a.par
order by a.par;
이 방법을 우아하게 해결할 수 있습니까? 감사.
E4가 예상되는 결과에 있지만 E8이 아닌 이유는 무엇입니까 – schurik