繁体   English   中英

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

[英]PL/SQL sum values in a column based on another columns value being

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

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;
/

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM