SELECT t.tipificacao1, t.tipificacao2, count(c.id) as Total0, 0 as Total1, 0 as Total2, 0 as Total3, 0 as Total4, 0 as Total5
FROM tipificacao as t
left JOIN chamadas as c ON c.idTipificacao = t.id
where c.created_at
BETWEEN '2014-10-13 00:00:00' AND '2014-10-19 23:59:59'
GROUP BY t.tipificacao1, t.tipificacao2
UNION
SELECT t.tipificacao1, t.tipificacao2, 0 as Total0, count(c.id) as Total1, 0 as Total2, 0 as Total3, 0 as Total4, 0 as Total5
FROM tipificacao as t
left JOIN chamadas as c ON c.idTipificacao = t.id
where c.created_at
BETWEEN '2014-10-20 00:00:00' AND '2014-10-26 23:59:59'
GROUP BY t.tipificacao1, t.tipificacao2
UNION
SELECT t.tipificacao1, t.tipificacao2, 0 as Total0, 0 as Total1, count(c.id) as Total2, 0 as Total3, 0 as Total4, 0 as Total5
FROM tipificacao as t
left JOIN chamadas as c ON c.idTipificacao = t.id
where c.created_at
BETWEEN '2014-10-27 00:00:00' AND '2014-11-2 23:59:59'
GROUP BY t.tipificacao1, t.tipificacao2
UNION
SELECT t.tipificacao1, t.tipificacao2, 0 as Total0, 0 as Total1, 0 as Total2, count(c.id) as Total3, 0 as Total4, 0 as Total5
FROM tipificacao as t
left JOIN chamadas as c ON c.idTipificacao = t.id
where c.created_at
BETWEEN '2014-11-3 00:00:00' AND '2014-11-9 23:59:59'
GROUP BY t.tipificacao1, t.tipificacao2
UNION
SELECT t.tipificacao1, t.tipificacao2, 0 as Total0, 0 as Total1, 0 as Total2, 0 as Total3, count(c.id) as Total4, 0 as Total5
FROM tipificacao as t
left JOIN chamadas as c ON c.idTipificacao = t.id
where c.created_at
BETWEEN '2014-11-10 00:00:00' AND '2014-11-16 23:59:59'
GROUP BY t.tipificacao1, t.tipificacao2
UNION
SELECT t.tipificacao1, t.tipificacao2, 0 as Total0, 0 as Total1, 0 as Total2, 0 as Total3, 0 as Total4, count(c.id) as Total5
FROM tipificacao as t
left JOIN chamadas as c ON c.idTipificacao = t.id
where c.created_at
BETWEEN '2014-11-10 00:00:00' AND '2014-11-10 23:59:59'
GROUP BY t.tipificacao1, t.tipificacao2
它返回:
created_at
我想要的是这个:
created_at
我怎么能这样做?
答案 0 :(得分:1)
只是封装查询并再次对其进行分组。
SELECT x.tipificacao1,x.tipificacao2,sum(x.Total0),sum(x.Total1),sum(x.Total2),sum(x.Total3),sum(x.Total4),sum(x.Total5)
FROM
(
---THAT WHOLE BIG QUERY OF YOURS COPYPASTE HERE---
) x
GROUP BY x.tipificacao1, x.tipificacao2
编辑以及另一个查询,测试哪一个为您运行更顺畅:
SELECT t.tipificacao1, t.tipificacao2,
count(c0.id) as Total0,count(c1.id) as Total1,count(c2.id) as Total2,count(c3.id) as Total3,count(c4.id) as Total4,count(c5.id) as Total5
FROM tipificacao as t
left JOIN chamadas as c0 ON c0.idTipificacao = t.id AND c0.created_at BETWEEN '2014-10-13 00:00:00' AND '2014-10-19 23:59:59'
left JOIN chamadas as c1 ON c1.idTipificacao = t.id and c1.created_at BETWEEN '2014-10-20 00:00:00' AND '2014-10-26 23:59:59'
left JOIN chamadas as c2 ON c2.idTipificacao = t.id and c2.created_at BETWEEN '2014-10-27 00:00:00' AND '2014-11-2 23:59:59'
left JOIN chamadas as c3 ON c3.idTipificacao = t.id and c3.created_at BETWEEN '2014-11-3 00:00:00' AND '2014-11-9 23:59:59'
left JOIN chamadas as c4 ON c4.idTipificacao = t.id and c4.created_at BETWEEN '2014-11-10 00:00:00' AND '2014-11-16 23:59:59'
left JOIN chamadas as c5 ON c5.idTipificacao = t.id and c5.created_at BETWEEN '2014-11-10 00:00:00' AND '2014-11-10 23:59:59'
WHERE 1
GROUP BY t.tipificacao1, t.tipificacao2