CASE语句数学并不总是有意义的

时间:2018-03-24 21:45:20

标签: mysql

我有一个SQL语句可以做一些数学运算。它计算基点,奖励积分和总积分(基数+奖金=总数)。

大部分时间(99%)的计算都符合预期。但有时返回的总数不是基数+奖金。我无法弄清楚为什么会这样。

这是相关的SQL

   SUM(CASE
        WHEN validated = 1 AND invalidated = 0
        THEN ptsBase
        ELSE 0
        END) AS awardedPoints,
    SUM(CASE
        WHEN validated = 1 AND invalidated = 0
        THEN ptsBonus
        ELSE 0
        END) AS awardedBonus,
    SUM(CASE
        WHEN validated = 1 AND invalidated = 0
        THEN ptsBase + ptsBonus
        ELSE 0
        END) AS awardedTotal,
  

注意:awardPoints和grantedBonus的计算总是准确的。据我所知,awardTotal计算是唯一一个关闭的计算。

在我正在看的情况下

  • awardPoints = 1345
  • awardBonus = 46
  • awardTotal = 1377(预计为1391,减少14分)

1 个答案:

答案 0 :(得分:0)

谢谢Martin Smith,McNets和CBroe。你们都钉了它。我正准备发布奖励积分栏有时候是空的。将代码更改为以下工作...

    SUM(CASE
        WHEN validated = 1 AND invalidated = 0
        THEN ptsBase
        ELSE 0
        END) AS awardedPoints,
    SUM(CASE
        WHEN validated = 1 AND invalidated = 0
        THEN ptsBonus
        ELSE 0
        END) AS awardedBonus,
    SUM(CASE
        WHEN validated = 1 AND invalidated = 0
        THEN ptsBase + COALESCE(ptsBonus, 0)
        ELSE 0
        END) AS awardedTotal,