This is the current query:
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;
And the result I want to get looks like this:
SUCCESS 5
FAILED 9
and that's ok. The problem is that I could get a case where the number of results is 0 for both statuses and in that case I want this:
SUCCESS 0
FAILED 0
and not an empty results set (what I get now). I have a general idea that I should either join the table on itself or use a WITH clause but I have no idea how to start that.
Try something like:
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
You can remove status.ct_reconcile_status IN ( 'SUCCESS', 'FAILED' ) AND
, but this could be slower.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With