with TA as
(select 'A' P1, 'B' P2,5 DI from dual union all
select 'A' P1, 'C' P2,6 DI from dual union all
select 'B' P1, 'D' P2,4 DI from dual union all
select 'B' P1, 'C' P2,3 DI from dual union all
select 'A' P1, 'E' P2,12 DI from dual union all
select 'B' P1, 'E' P2,4 DI from dual union all
select 'C' P1, 'E' P2,8 DI from dual union all
select 'D' P1, 'F' P2,7 DI from dual union all
select 'E' P1, 'F' P2,5 DI from dual
)
自己琢磨的,请指点with TA as
(select 'A' P1, 'B' P2,5 DI from dual union all
select 'A' P1, 'C' P2,6 DI from dual union all
select 'B' P1, 'D' P2,4 DI from dual union all
select 'B' P1, 'C' P2,3 DI from dual union all
select 'A' P1, 'E' P2,12 DI from dual union all
select 'B' P1, 'E' P2,4 DI from dual union all
select 'C' P1, 'E' P2,8 DI from dual union all
select 'D' P1, 'F' P2,7 DI from dual union all
select 'E' P1, 'F' P2,5 DI from dual
),
TB as
(select 'A' || SYS_CONNECT_BY_PATH(p2, '_') R
from ta
where P2 = 'F'
start with p1 = 'A'
connect by p1 = prior p2)select R,sum(DI)
from ta, tb
where regexp_like(R, P1 || '_' || P2 )
group by R
order by sum(di)