我有这个查询生成我想要的结果。 我只需要将日期列入专栏
SELECT item, date, SUM(quantity)
FROM t
WHERE date between '2015-08-18' and '2015-08-20'
GROUP BY item, date
这是我的SQL FIDDLE
生成
结果我想要
请任何人至少能告诉我如何达到我想要的结果吗?
答案 0 :(得分:3)
使用conditional Aggregate
从day
使用DAY()函数
date
SELECT item,
SUM(case when day(date) = 18 then quantity else 0 end) as `18`,
SUM(case when day(date) = 19 then quantity else 0 end) as `19`,
SUM(case when day(date) = 20 then quantity else 0 end) as `20`
FROM t
WHERE date between '2015-08-18' and '2015-08-20'
GROUP BY item