数据库表comms,有3个字段:
id name parentid
11 煤炭 null
1101 烟煤优混 11
1102 无烟煤优混 11
12 成品油 null
1201 柴油 12
120101 柴油0# 1201
1202 汽油 12
120201 汽油93# 1202我想要通过sql计算出每个品种是否是最末级,每个大类分几级不确定。目标结果:
id name parentid isEndLevel
11 煤炭 null 0
1101 烟煤优混 11 1
1102 无烟煤优混 11 1
12 成品油 null 0
1201 柴油 12 0
120101 柴油0# 1201 1
1202 汽油 12 0
120201 汽油93# 1202 1
id name parentid
11 煤炭 null
1101 烟煤优混 11
1102 无烟煤优混 11
12 成品油 null
1201 柴油 12
120101 柴油0# 1201
1202 汽油 12
120201 汽油93# 1202我想要通过sql计算出每个品种是否是最末级,每个大类分几级不确定。目标结果:
id name parentid isEndLevel
11 煤炭 null 0
1101 烟煤优混 11 1
1102 无烟煤优混 11 1
12 成品油 null 0
1201 柴油 12 0
120101 柴油0# 1201 1
1202 汽油 12 0
120201 汽油93# 1202 1
Create or replace function F_IsEndLevel(Pid in number) return int
is
pid int;
begin
select count(*) from Ytab where Parentid=pid;
--pid表示需要调用的行的id
if Pid=0 then
return 1;
else
return 0;
end if;
end;
还是谢谢你!
select id,pid,F_IsEndLevel(id) from Ytab;
--如果要再简化,可以这样。
select id,pid,(select case count(*) when 0 then 1 else 0
from Ytab b where b.id=a.id) as IsEndLevel
From Ytab;
where not exists
(select 1 from comms b
where b.parentid=a.id);
from Ytab b where b.id=a.id) as IsEndLevel
From Ytab;
b.id=a.id中的a.id是什么??
要么就是用sql动态查询
is
pid int;
begin
select count(*) from espc_mmp_comms_gnxh where Parentid=pid;
--pid表示需要调用的行的id
if pid=0 then
return 1;
else
return 0;
end if;
end; 执行这个报错
Compilation errors for FUNCTION MMS.F_ISENDLEVELError: PLS-00410: RECORD,TABLE 或参数列表中的字段不允许重复
Line: 1
Text: create or replace function F_IsEndLevel(pid in number) return intError: PL/SQL: Compilation unit analysis terminated
Line: 0
Text: create or replace function F_IsEndLevel(pid in number) return int我不太懂oracle的函数,请楼上大哥帮忙啊!
用minus解决了!
(select c.id from espc_mmp_comms_gnxh c) minus (select d.parentid id from espc_mmp_comms_gnxh d where d.parentid is not null)