1

这是我的mysql查询

    SELECT IntervalStartTime,IFNULL(SUM(AbandonedCalls),0) AS AbandonedCallSum,SUM(QueueTime) AS QTS,SUM(RingTime) AS RTS,
IFNULL(SUM(AnsweredCalls),0) AS AnsweredCallSum
FROM intervalqueuestatistics
WHERE CallCenterId=17 AND DATE_FORMAT(IntervalStartTime,'%m')=10 AND DATE_FORMAT(IntervalStartTime,'%Y')=2012
GROUP BY DATE_FORMAT(IntervalStartTime,'%d');

现在我想计算一个值(SUM(QueueTime)+SUM(RingTime))/SUM(AnsweredCalls)

所以我相应地修改了我的查询,如下所示

    SELECT IntervalStartTime,IFNULL(SUM(AbandonedCalls),0) AS AbandonedCallSum,SUM(QueueTime) AS QTS,SUM(RingTime) AS RTS,
IFNULL(SUM(AnsweredCalls),0) AS AnsweredCallSum,IFNULL(SUM(QueueTime),0) + IFNULL(SUM(RingTime),0)/IFNULL(SUM(AnsweredCalls),0)
FROM intervalqueuestatistics
WHERE CallCenterId=17 AND DATE_FORMAT(IntervalStartTime,'%m')=10 AND DATE_FORMAT(IntervalStartTime,'%Y')=2012
GROUP BY DATE_FORMAT(IntervalStartTime,'%d');

但是当执行它并没有给我正确的答案。

例如此查询返回的行之一

 QTS    RTS   AnsweredCallSum CalculatedField 
188000  41645        9          192627.222

但是CalculatedField是错误的,根据上面的计算应该是25516.11

4

2 回答 2

1

如果 SUM(AnsweredCalls) 为 0,则您除以 0,它不应该工作。

于 2012-12-10T14:27:19.663 回答
0

这将是我的猜测:

  1. 使用,因此如果存在空列IFNULL(..., 1),您永远不会除以。0
  2. 显式使用括号,这样操作顺序就不会失败。(sum1 + (sum2 / sum3)当我认为你想要的时候,你目前正在表演(sum1 + sum2) / sum3)。记住乘法和除法在加法和减法之前没有括号。

( IFNULL(SUM(QueueTime), 0) + IFNULL(SUM(RingTime), 0) )
        / IFNULL(SUM(AnsweredCalls), 1)
于 2012-12-10T14:32:06.260 回答