select sum(sal) as jia from gongziTable where bumen = '1' and jianngjin = 'jia'
select sum(sal) from kou gongziTable where bumen = '1' and jianngjin = 'kou'
(最后 jia - kou )这是我想要的值,能不能一条语句解决?
如何将上面的 两条语句修改成一条语句查询出来?
一个部门的 (奖金添加) 减去 (奖金扣除) 的值 在一条sql语句中解决。
感谢回答,这种写法真是第一次见到!
我又扩展了一下,将以前查询6次的方式 换成现在的一条语句了。
select
nvl ( sum(case when z.dztfm = '1' and z.cpbz = '1' then to_number(s.cpfs) when z.dztfm = '1' and z.cpbz = '2' then -to_number(s.cpfs) else 0 end) ,0)
, nvl ( sum(case when z.dztfm = '2' and z.cpbz = '1' then to_number(s.cpfs) when z.dztfm = '2' and z.cpbz = '2' then -to_number(s.cpfs) else 0 end) ,0)
, nvl ( sum(case when z.dztfm = '3' and z.cpbz = '1' then to_number(s.cpfs) when z.dztfm = '3' and z.cpbz = '2' then -to_number(s.cpfs) else 0 end) ,0)
from sis_cp_sssqzhjfcp s,sis_cp_zhjfcp z where s.cpbh =z.resource_id
select sum(case when jiangjin='jia' then sal when jiangjin='kou' then -sal else 0 end) as jiakou from gongziTable where bumen='1'
做法就是楼上的方法,是否正确要自己去调试