这并不完美,但可能是一个开始:
首先我创建了一个函数:
create or replace function sum_string(str in varchar2) return number is
Result number;
begin
execute immediate 'select ' || str || ' from dual'
into result;
return(Result);
end sum_string;
/
然后是查询(your_table
是你的表)
with jwt as (
select t1.key k1, t1.name n1, t2.key k2, t2.name n2, t1.key + t2.key jkey
from your_table t1
left outer join (select key, name
from your_table
union all
select 0 "key", null "name" from dual) t2 on t1.key <>
t2.key
)
select jkey, max(aaa.pth)
from (select sys_connect_by_path(t.n1, '/') pth,
sum_string(ltrim(sys_connect_by_path(t.k1, '+'), '+')) jkey
from jwt t
start with t.k2 = 0
connect by nocycle prior k1 = k2) aaa
group by jkey;