2017-08-03 69 views
1

試圖在MySQL中編寫一個查詢,該查詢需要執行由特定deviceToken執行的4個不同操作,並在特定時間範圍內並返回action,deviceTokencount採取了多少次這一行動。我的問題是,爲了正確處理結果,我總是需要所有4個動作成爲回報的一部分,即使計數爲0,並且GROUP BY臭名昭着地不會與此一起玩。下面是該查詢我目前:當GROUP BY不產生匹配時,返回一行數爲0

SELECT `action`, 
     deviceToken, 
     CASE WHEN `action` = 'calendar' THEN COUNT(*) END AS CalCount, 
     CASE WHEN `action` = 'closinginfo' THEN COUNT(*) END AS CloseingCount, 
     CASE WHEN `action` = 'generalinfo' THEN COUNT(*) END AS GenInfoCount, 
     CASE WHEN `action` = 'login' THEN COUNT(*) END AS LoginCount 
FROM `action` 
WHERE deviceToken = '44262f82-3767-49c8-8786-ee377ea00a64' 
    AND (`action` = 'login' OR `action` = 'calendar' OR `action` = 'closinginfo' OR `action` = 'generalinfo') 
    AND `timestamp` > CONCAT(DATE(NOW()),' 23:59:59') - INTERVAL DAYOFWEEK(NOW())+14 DAY 
    AND `timestamp` < CONCAT(DATE(NOW()),' 23:59:59') - INTERVAL DAYOFWEEK(NOW())+7 DAY 
GROUP BY `action` 

對於已經在這一時限內完成所有4個動作deviceToken,結果是這樣的:

enter image description here

的方式我的客戶端處理這個結果已經很完美了。然而,已經採取少於所有4個動作deviceToken看起來是這樣的:

enter image description here

我需要爲我的數據處理,以普遍的工作仍然是返回那些失蹤行,但只列出那些算作0.有沒有人有任何想法如何做到這一點?

+0

當你在第一列中有動作時,你是否真的需要計算不同列的不同動作? – Barmar

回答

1

在MySQL布爾表達式解析爲1或0,這樣就可以SUM他們:

SELECT 
     SUM(`action` = 'calendar') AS CalCount, 
     SUM(`action` = 'closinginfo') AS CloseingCount, 
     SUM(`action` = 'generalinfo') AS GenInfoCount, 
     SUM(`action` = 'login') AS LoginCount 
FROM `action` 
WHERE deviceToken = '44262f82-3767-49c8-8786-ee377ea00a64' 
    AND (`action` = 'login' OR `action` = 'calendar' OR `action` = 'closinginfo' OR `action` = 'generalinfo') 
    AND `timestamp` > CONCAT(DATE(NOW()),' 23:59:59') - INTERVAL DAYOFWEEK(NOW())+14 DAY 
    AND `timestamp` < CONCAT(DATE(NOW()),' 23:59:59') - INTERVAL DAYOFWEEK(NOW())+7 DAY 

注意這個查詢不使用GROUP BY,僅聚集體在SELECT

+0

這會爲我生成一個錯誤。 'FUNCTION analytics.SUM不存在。檢查參考手冊中的'Function Name Parsing and Resolution'部分' – Jodo1992

+1

好吧,這只是一個語法問題。在MySQL函數的括號之前不能是空格。所有的設置,這個解決方案是優雅的,並且工作得很好,謝謝。 – Jodo1992

0

使用LEFT JOIN用一個表格列出所有這些行爲空值的動作。

SELECT t1.`action`, 
     '44262f82-3767-49c8-8786-ee377ea00a64' AS deviceToken, 
     CASE WHEN t1.`action` = 'calendar' THEN COUNT(a.action) END AS CalCount, 
     CASE WHEN t1.`action` = 'closinginfo' THEN COUNT(a.action) END AS CloseingCount, 
     CASE WHEN t1.`action` = 'generalinfo' THEN COUNT(a.action) END AS GenInfoCount, 
     CASE WHEN t1.`action` = 'login' THEN COUNT(a.action) END AS LoginCount 
FROM (SELECT 'calendar' AS action 
     UNION ALL 
     SELECT 'closinginfo' 
     UNION ALL 
     SELECT 'generalinfo' 
     UNION ALL 
     SELECT 'login') AS t1 
LEFT JOIN `action` AS a 
ON a.action = t1.action 
    AND deviceToken = '44262f82-3767-49c8-8786-ee377ea00a64' 
    AND `timestamp` > CONCAT(DATE(NOW()),' 23:59:59') - INTERVAL DAYOFWEEK(NOW())+14 DAY 
    AND `timestamp` < CONCAT(DATE(NOW()),' 23:59:59') - INTERVAL DAYOFWEEK(NOW())+7 DAY 
GROUP BY t1.`action` 
+0

這是導致那些通常不會返回顯示的三行,而是計數爲0,而不是計數爲1 – Jodo1992

+0

是的,需要將'COUNT(*)'改爲'COUNT(a.action)' 。您必須計算「a」表中的列,以便不計算具有空匹配的行。 – Barmar