基于另一列值的列中的PL / SQL总和值

时间:2013-09-20 17:55:21

标签: sql oracle plsql oracle11g

我只是在津贴为零时尝试显示费用,而我的另一个问题是,如果我在津贴列中有多个零, 我只想把这些费用加起来。那个不是零的那个我不想把它们包含在总和中。

Allowa  charges
-----------------------------------------------
84.27   90  0   188428752   2012-05-17 00:00:00
0       100 0   188428752   2013-08-08 00:00:00
84.27   90  0   188428752   2012-06-14 00:00:00
124.7   130 0   188428752   2012-05-10 00:00:00
90.79   90  0   188428752   2012-08-09 00:00:00
90.79   100 0   188428752   2012-10-11 00:00:00
90.79   100 0   188428752   2013-05-23 00:00:00

费用应为100

Allowa  charges
-------------------------------------------------
  0       100     188428752   2013-08-08 00:00:00
  0       90      188428752   2012-06-14 00:00:00
  90.79   100 0   188428752   2012-10-11 00:00:00
  90.79   100 0   188428752   2013-05-23 00:00:00

费用应为190.

这个pl / sql只给我总和。

create or replace procedure summary
as
cursor c1
is
select allowance,sum(charges) CH,sum(not_paid) np ,patientid
from claims1 a, claim_details b
where a.claimid = b.claimid
group by allowance, charges,not_paid,patientid;

ins_bal number(9,2);
pat_bal number(9,2);

begin

for a1 in c1 loop

if a1.allowance = 0 then
ins_bal:=a1.CH;
end if;

if a1.allowance != 0 then
 pat_bal:=a1.np;
end if;

insert into summary_hold values(ins_bal,pat_bal,a1.patientid);
commit;

end loop;
end;
/

 This is the final pl/sql
create or replace procedure summary
as
cursor c1
is
select allowance,(case nvl(allowance,0)  when 0 then sum(charges) else 0 end)CH,
(case when nvl(not_paid,0) <> 0 then sum(not_paid) else 0 end) np,patientid,datefrom
from claims1 a, claim_details b
where a.claimid = b.claimid
group by allowance, charges,not_paid,patientid,datefrom;


begin

for a1 in c1 loop

insert into summary_hold values(a1.ch,a1.np,a1.patientid);
commit;

end loop;
end;
/

1 个答案:

答案 0 :(得分:2)

只需使用CASE语句即可。 sum(case Allowa when 0 then charges else 0 end)