2014-07-10 71 views
2

從UX post

感動我有2個疑問,在每一個捲起來..選擇彙總後加入


SELECT DATE(date) AS day, 
     COUNT(IF(name = 'red', 1, NULL)) AS "red", 
     COUNT(IF(name = 'blue', 1, NULL)) AS "blue", 
     COUNT(IF(name = 'yellow', 1, NULL)) AS "yellow" 
    FROM test1 
GROUP BY day with rollup 

SELECT DATE(date) AS day, 
     COUNT(*) AS total 
    FROM test2 
GROUP BY day with rollup 

當加入他們rollup行被刪除,所以我通過使用另一個來解決它查詢計算rollupunion它的結果

這裏結束的現場演示 [sqlfiddle],約我做的方式有什麼想法這(最喜歡的練習或表演),如果有一種方法來保持rollup後加入,我不知道。任何指針讚賞。

更新

我想要的正確結果集是小提琴的結果。每行包含一天的摘要,從第一個查詢項目/顏色數量和第二個查詢當天的請求數量。彙總將是每個項目/顏色和請求的分類總數。希望我明確了:)

+0

你需要什麼結果集?每一行都有什麼?捲起行應該是什麼? –

+0

@OllieJones請檢查更新後的帖子:)謝謝 – Katia

回答

2

我不認爲「彙總」被刪除。結果行不符合join的條件。你可以試試這個:

select t1.*, t2.total 
from (SELECT date(date) as day, count(IF(name = 'red', 1, NULL)) AS "red", 
      sum(name = 'blue') AS blue, 
      sum(name = 'yellow') AS yellow 
     FROM test1 
     group by day with rollup 
    ) t1 join 
    (select date(date) as day, count(*) as total 
     from test2 
     group by day with rollup 
    ) t2 
    on t1.day = t2.day or t1.day is null and t2.day is null; 
+0

,它像魔術一樣工作。謝謝你的提示 – Katia