3

这是当前查询:

SELECT status.ct_reconcile_status, 
       IFNULL(COUNT(status.ct_reconcile_status), 0) AS sch_change_status_num 
FROM   db_crewops_cm_action status 
WHERE  status.ct_reconcile_status IN ( 'SUCCESS', 'FAILED' ) 
       AND status.updated_ts > UTC_TIMESTAMP() - INTERVAL 60 minute 
GROUP  BY status.ct_reconcile_status; 

我想要得到的结果是这样的:

SUCCESS   5
FAILED    9

没关系。问题是我可能会遇到两种状态的结果数都是 0 的情况,在这种情况下我想要这个:

SUCCESS   0
FAILED    0

而不是一个空的结果集(我现在得到的)。我有一个一般的想法,我应该自己加入表或使用 WITH 子句,但我不知道如何开始。

4

1 回答 1

3

尝试类似:

SELECT a.status AS ct_reconcile_status,
       (IFNULL(b.sch_change_status_num,0) + a.num) AS sch_change_status_num
FROM
  (SELECT 'SUCCESS' AS status, 0 AS num
   UNION
   SELECT 'FAILED', 0) a
LEFT JOIN
  (SELECT status.ct_reconcile_status, 
          COUNT(status.ct_reconcile_status) AS sch_change_status_num 
   FROM   db_crewops_cm_action status 
   WHERE  status.ct_reconcile_status IN ( 'SUCCESS', 'FAILED' ) 
          AND status.updated_ts > UTC_TIMESTAMP() - INTERVAL 60 minute 
   GROUP  BY status.ct_reconcile_status) b
ON a.status = b.ct_reconcile_status

您可以删除status.ct_reconcile_status IN ( 'SUCCESS', 'FAILED' ) AND,但这可能会更慢。

于 2013-02-01T11:08:16.723 回答