我有一张如下表格
Date | Product | Qty
-------------|---------------|------
12-Dec-12 | reference1 | 1
14-Dec-12 | reference2 | 2
14-Dec-12 | reference1 | 3
1-Jan-13 | reference2 | 4
3-Jan-13 | reference2 | 5
3-Jan-13 | reference3 | 6
我希望通过查询
得到它Product | Dec 2012 | Jan 2013
===========|============|==========
reference1 | 4 | 0
reference2 | 2 | 9
reference3 | 0 | 6
我知道如何分组,我的问题是如何设置动态列(我希望能够选择上一个6 months
,12 months
或24 months
)。
答案 0 :(得分:2)
您正在尝试将数据从行转移到列中。 MySQL没有透视功能,但您可以使用带CASE
的聚合函数来获得结果:
select product,
sum(case when month(date) = 12 and year(date) = 2012
then qty else 0 end) Dec2012,
sum(case when month(date) = 1 and year(date) = 2013
then qty else 0 end) Jan2013
from yourtable
group by product
也可以使用子查询编写,以日月格式获取日期:
select product,
sum(case when MonthYear = 'Dec_2012' then qty else 0 end) Dec2012,
sum(case when MonthYear = 'Jan_2013' then qty else 0 end) Jan2013
from
(
select product,
date_format(date, '%b_%Y') MonthYear,
qty
from yourtable
) src
group by product;
然后,如果您想要动态生成日期列表或者要返回未知数量的日期,则可以使用预准备语句生成动态SQL:
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'sum(case when MonthYear = ''',
MonthYear,
''' then qty else 0 end) AS ',
MonthYear
)
) INTO @sql
FROM
(
select product,
date_format(date, '%b_%Y') MonthYear,
qty
from yourtable
) src;
SET @sql = CONCAT('SELECT product, ', @sql, '
from
(
select product,
date_format(date, ''%b_%Y'') MonthYear,
qty
from yourtable
) src
GROUP BY product');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
见SQL Fiddle with Demo。这三个人都会给你结果:
| PRODUCT | DEC_2012 | JAN_2013 |
------------------------------------
| reference1 | 4 | 0 |
| reference2 | 2 | 9 |
| reference3 | 0 | 6 |