我在mysql中有表,如下面的
表格数据
id items color date
1 22 blue 2014-05-25
2 344 yellow 2014-05-26
3 255 blue 2014-05-26
4 234 blue 2014-05-26
5 200 yellow 2014-05-27
6 100 blue 2014-05-27
7 100 yellow 2014-05-25
8 233 blue 2014-05-26
我需要基于颜色的项目总和,按日期分组...
喜欢
我需要这样的输出:
Date Items blue yellow
2014-05-25 122 22 100
2014-05-26 1066 722 344
2014-05-27 300 100 200
我需要Mysql Query来获取上面给出的输出..
答案 0 :(得分:1)
您可以按日期进行正常分组 并根据http://dev.mysql.com/doc/refman/5.0/en/case.html 你应该
select Date, sum(items) as Items,
sum(case when color = 'blue' then color else 0 end case) as blue,
sum(case when color = 'yellow' then color else 0 end case) as yellow
from ...
group by Date
很抱歉,如果语法不匹配。我不做MySQL。
答案 1 :(得分:0)
你可以这样做:
SELECT
Table1.Date,
SUM(CASE WHEN Table1.color='blue' THEN Table1.items ELSE 0 END) AS blue,
SUM(CASE WHEN Table1.color='yellow' THEN Table1.items ELSE 0 END) AS yellow,
SUM(Table1.items) AS Items
FROM
Table1
GROUP BY
Table1.Date
答案 2 :(得分:0)
试试这个..
SELECT date, sum(items), sum(if(color='blue',items,0)) as blue, sum(if(color='yellow',items,0)) as yellow
FROM `Table`
GROUP BY date
ORDER BY date ASC