0

这是mysql查询

SELECT CallDate,SUM(Hour0) AS FailedCalls FROM call_status WHERE CallDate='2012-09-14' AND CallStatusId NOT IN (42,52)

UNION

SELECT CallDate,SUM(Hour0) AS SuccessCalls FROM call_status WHERE CallDate='2012-09-14' AND CallStatusId IN (42,52);

这是输出 在此处输入图像描述

抱歉图片太小了。如您所见,我从上面的查询中选择了失败的呼叫,并使用第二个查询选择了 SuccessCalls。但我想要的是 CallDate、FailedCalls、SuccessCalls 在一行中。

更新: 这是正确的查询:

SELECT CallDate,
SUM(CASE WHEN (CallStatusId NOT IN(42,52)) THEN Hour0
ELSE 0 
END) AS FailedCalls,
SUM(CASE WHEN (CallStatusId IN (42,52)) THEN Hour0
ELSE 0
END) AS SuccessCalls
FROM call_status
WHERE CallDate='2012-09-14';
4

1 回答 1

0

这些方面的东西?

SELECT CallDate
      ,SUM( CASE WHEN (Hour0 != 42 AND Hour0 != 52) THEN 1 ELSE 0 END ) AS FailedCalls
      ,SUM( CASE WHEN (Hour0  = 42  OR Hour0  = 52) THEN 1 ELSE 0 END ) AS SuccessCalls
  FROM call_status
  WHERE CallDate='2012-09-14'
于 2012-10-05T11:55:46.723 回答