Mysql
 sql >> Database >  >> RDS >> Mysql

Come posso ottenere un set di risultati non vuoto quando utilizzo select, group by e count?

Prova qualcosa come:

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

Puoi rimuovere status.ct_reconcile_status IN ( 'SUCCESS', 'FAILED' ) AND , ma potrebbe essere più lento.