作为列的日期(每天创建报告)

时间:2015-08-23 14:47:12

标签: php mysql sql

我有这个查询生成我想要的结果。 我只需要将日期列入专栏

SELECT item, date, SUM(quantity) 
FROM t 
WHERE date between '2015-08-18' and '2015-08-20'
GROUP BY item, date

这是我的SQL FIDDLE

生成

Output

结果我想要

result

请任何人至少能告诉我如何达到我想要的结果吗?

1 个答案:

答案 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

SQL FIDDLE DEMO