日期:2014-05-17  浏览次数:20510 次

sql 2005 sum统计问题
表结构:表名(table)
id number num
1 5 20
2 10 10
3 1 2
4 3 7
5 9 8
6 -4 6
...

现在要分别统计number(只统计大于0的),num的和
select sum(number) from table where number>0 没问题
select sum(num) from table 没问题
我想一条sql语句就统计,于是我这样写
select sum(a.number),sum(b.num) from table as a,table as b where a.number>0
这样的话 计算出来的结果就出错了,这是为什么?



------解决方案--------------------
SQL code

select
    sum(case when number>0 then number else 0 end) [大于0],
    sum(num) [总和]--此列看错了,按你的表应是这个
from [table];