SQL DB2 null 计算导致问题
我有以下 SQL:
Select dmvndn "Vendor Number", IFNULL(sum(dmsls) / sum(dmprc), 0) "Calculation"
From MyFile
Group By dmvndn
但是,当我运行此命令时,我的“计算”字段中仍然收到空记录。
我还尝试了 COALESCE 函数,它返回相同的结果。我得到的一些记录为 0,一些记录为空(或 null)。
这两个字段都是 P 类型,我被告知它是压缩数字。
有什么想法或建议吗?
编辑1 看来问题不在于这些字段中的任何一个为 NULL,而在于一个或两个字段为 0。当我除以零时,我得到空/空白结果。
I have the following SQL:
Select dmvndn "Vendor Number", IFNULL(sum(dmsls) / sum(dmprc), 0) "Calculation"
From MyFile
Group By dmvndn
However, when i run this, i am still getting null records in my "Calculation" field.
I have also tried the COALESCE function, which returns the same results. I get some records as 0, and some records are blank (or, null).
Both fields are of type P, which i am told is packed numeric.
any ideas or suggestions?
Edit 1
It seems that the problem is not with either of these fields being NULL, it is that one or both fields are 0. And when i divide by zero, i get the empty / blank result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
Try
这种技巧在 MS SQL Server 中对我有帮助:
我想知道它在 DB2 中是否对您没有帮助。
更新:解释。
基本上,如果除数为 0,它会将除数替换为 NULL。您可能知道,当至少一个操作数为 NULL 时,运算结果也将变为 NULL。
为了解释结果为 NULL,您已经在结果上设置了 IFNULL。 然后并没有多大区别,因为没有一个操作数可能是 NULL。然而,现在使用 IFNULL 非常有意义。
A trick of this kind helps me in MS SQL Server:
I wonder if it can't help you in DB2.
UPDATE: an explanation.
Basically, it replaces the divisor with NULL if it's 0. And you may probably know that when at least one of the operands is NULL, the result of the operation becomes NULL as well.
To account for the result being NULL you already had your IFNULL on the result. It didn't make much difference then, because none of the operands was likely to be NULL. However, now using IFNULL makes perfect sense.