SQL DB2 null 计算导致问题

发布于 2024-11-01 11:32:08 字数 389 浏览 6 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百合的盛世恋 2024-11-08 11:32:08

尝试

Sum(IFNULL(dmsls,0)) / Sum(IFNULL(dmprc,0))

Try

Sum(IFNULL(dmsls,0)) / Sum(IFNULL(dmprc,0))
夏花。依旧 2024-11-08 11:32:08

这种技巧在 MS SQL Server 中对我有帮助:

Select
  dmvndn "Vendor Number",
  IFNULL(sum(dmsls) / NULLIF(sum(dmprc), 0), 0) "Calculation"
From MyFile
Group By dmvndn

我想知道它在 DB2 中是否对您没有帮助。

更新:解释。

基本上,如果除数为 0,它会将除数替换为 NULL。您可能知道,当至少一个操作数为 NULL 时,运算结果也将变为 NULL。

为了解释结果为 NULL,您已经在结果上设置了 IFNULL。 然后并没有多大区别,因为没有一个操作数可能是 NULL。然而,现在使用 IFNULL 非常有意义。

A trick of this kind helps me in MS SQL Server:

Select
  dmvndn "Vendor Number",
  IFNULL(sum(dmsls) / NULLIF(sum(dmprc), 0), 0) "Calculation"
From MyFile
Group By dmvndn

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文