sum求和的问题

accps1 2009-12-22 03:22:17
数据结构如下(注:数据库为DB2)
id money money2 money3.....
1 11 12
2 NULL NULL
3 13 NULL

现在的问题是 sum(money+money2+money3...) 求和时,如果存在一个字段为null的情况则求和的结果即为null.
在sql server 中可以这样解决: sum( isnull(money,0)+isnull(money2,0)...),现在问题是DB2中没有这种函数
所以只有单个字段求和再通过程序累加,很麻烦,请问高手有何解决办法?
...全文
128 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
accps1 2009-12-22
  • 打赏
  • 举报
回复
coalesce函数正确,非常感谢! 马上结贴
qurihong 2009-12-22
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 vivian_lanlan 的回复:]
coalesce函数.ansi标准.
[/Quote]
正解
accps1 2009-12-22
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 sgtzzc 的回复:]
SQL codeselect
id,sum(casewhenmoneyisnullthen0elsemoneyend+casewhen money2isnullthen0else money2end+casewhen money3isnullthen0else money3end
)from tbgroupby id
[/Quote]

四方兄这种方法我也考虑过,但现在我要求和的字段很多不太方便
--小F-- 2009-12-22
  • 打赏
  • 举报
回复
DB2 学习一下
gao__910 2009-12-22
  • 打赏
  • 举报
回复
帮顶
sgtzzc 2009-12-22
  • 打赏
  • 举报
回复
select 
id,
sum(case when money is null then 0 else money end
+case when money2 is null then 0 else money2 end
+case when money3 is null then 0 else money3 end
)
from tb
group by id
vivian_lanlan 2009-12-22
  • 打赏
  • 举报
回复
coalesce函数.ansi标准.
Gambling_ 2009-12-22
  • 打赏
  • 举报
回复
DB2
UP
dawugui 2009-12-22
  • 打赏
  • 举报
回复
肯定有.不过不知道是哪个,帮顶.

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧