首页 新闻 赞助 找找看

sql 同一个表的计算问题!

0
[已解决问题] 解决于 2012-11-17 15:53

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

不经意的回眸的主页 不经意的回眸 | 菜鸟二级 | 园豆:228
提问于:2012-11-16 09:45
< >
分享
最佳答案
0
select sum(case when jiangjin='jia' then sal when jiangjin='kou' then -sal else 0 end) as jiakou
from 
gongziTable
where bumen='1'
奖励园豆:5
飞来飞去 | 老鸟四级 |园豆:2057 | 2012-11-16 09:54
其他回答(1)
0

做法就是楼上的方法,是否正确要自己去调试

jason2013 | 园豆:1998 (小虾三级) | 2012-11-16 16:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册