一个简单的的问题,请大家看看
表
table1
userid msgcount
1 10
2 23
3 null
4 2
5 null
现在要统计出msgcount的总 数量
不知道SQL 的语句怎么写。
select sum(isnull(msgcount,0)) from table
select sum(case when msgcount is null then 0 else msgcount end)
from table
select sum(isnull(msgcount,0)) from tablename
no need to do isnull check:
select sum(msgcount) as sum from table1
直接SUM就可以了,不用ISNULL,即使用了这种用法也没意义
select sum(msgcount) as sum from table1
同意:mnjrh(SQL太难了)
直接用SUM,如果考虑可能会没有匹配的记录供聚合,返回的值为NULL,那么就在SUM外加个ISNULL就行
ISNULL有意义,但要加在最外面,如果没有数据,或者所有的数据都是NULL时,SUM计算的结果是NULL