3,491
社区成员
发帖
与我相关
我的任务
分享
--(1)
with temp as(
select 'a' parent,'b' child,3 quty from dual
union all select 'a','c',1 from dual
union all select 'b','d',4 from dual
union all select 'b','e',1 from dual
union all select 'b','f',4 from dual
union all select 'e','h',2 from dual)
select child, func_test2(substr(sys_connect_by_path(quty, '*'), 2)) num
from temp t
where connect_by_isleaf = 1
start with parent = 'a'
connect by prior child = parent;
--(2)
with temp as(
select 'a' parent,'b' child,3 quty from dual
union all select 'a','c',1 from dual
union all select 'b','d',4 from dual
union all select 'b','e',1 from dual
union all select 'b','f',4 from dual
union all select 'e','h',2 from dual)
select child,
sum(func_test2(substr(sys_connect_by_path(quty, '*'), 2))) num
from temp t
where connect_by_isleaf = 1
start with parent in ('a', 'b')
connect by prior child = parent
group by child;
create or replace function func_test2(str in varchar2)
return number
as
num number;
begin
execute immediate 'select '||str||' from dual' into num;
return num;
end;
with temp as(
select 'a' parent,'b' child,3 quty from dual
union all select 'a','c',1 from dual
union all select 'b','d',4 from dual
union all select 'b','e',1 from dual
union all select 'b','f',4 from dual
union all select 'e','h',2 from dual)
--以上为测试数据
select child,
func_test2(substr(sys_connect_by_path(quty,'*'),2))num
from temp t
where connect_by_isleaf=1
start with not exists(
select 1 from temp where child=t.parent)
connect by prior child=parent