mysql,转置/枢轴行到列,变量选择
再次您好,提前感谢您的帮助.
我检查了几个先前的问题,但找不到确切的情况.
我正在尝试将行转置/转置到列,但结果基于 where 子句中的日期函数,这使得我的选择有些可变.
<上一页>选择DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') as Install_Date,计数(packet_details.installDate)从数据包详细信息在哪里packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAYAND packet_details.installDate "*lessthan*" CURRENT_DATE + INTERVAL 7 DAY按安装日期分组*小于符号不会显示在堆栈上,我不知道如何修复它不确定这是否有意义,所以我加入了一个小提琴:http://sqlfiddle.com/#!2/5b235/3/0所以是这样的:
安装日期 COUNT2013 年 1 月 24 日 22013 年 1 月 25 日 22013 年 1 月 26 日 22013 年 1 月 27 日 22013 年 1 月 28 日 22013 年 1 月 29 日 12013 年 2 月 3 日 12013 年 2 月 4 日 12013 年 2 月 5 日 52/6/2013 4
变成:
<上一页>安装日期 1/24/2013 1/25/2013 1/26/2013 1/27/2013 1/28/2013....计数 2 2 2 2 2 1 解决方案SELECT Install_DATE,MAX(安装日期 = '01-24-2013' THEN totalCount END)'01-24-2013',MAX(安装日期 = '01-25-2013' THEN totalCount END)'01-25-2013',MAX(安装日期 = '01-26-2013' THEN totalCount END)'01-26-2013',…………从(选择 DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') 作为 Install_Date,Count(packet_details.installDate) totalCountFROM packet_detailsWHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY ANDpacket_details.installDate <CURRENT_DATE + INTERVAL 7 天按安装日期分组) 年代
- SQLFiddle 演示
对于未知数量的Install_Date
,动态查询是更可取的,
SET @sql = NULL;选择GROUP_CONCAT(DISTINCT连接('MAX(CASE WHEN Install_DATE = ''',安装日期,''' 然后 totalCount end) AS `', Install_Date, '`' )) 进入@sql从(选择 DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') 作为 Install_Date,Count(packet_details.installDate) totalCountFROM packet_detailsWHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY ANDpacket_details.installDate <CURRENT_DATE + INTERVAL 7 天按安装日期分组) 小号;SET @sql = CONCAT('SELECT Install_DATE, ', @sql, '从(选择 DATE_FORMAT(packet_details.installDate,''%m-%d-%Y'') 作为 Install_Date,Count(packet_details.installDate) totalCountFROM packet_detailsWHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY ANDpacket_details.installDate <CURRENT_DATE + INTERVAL 7 天按安装日期分组) s');从@sql 准备stmt;执行语句;DEALLOCATE PREPARE stmt;
- SQLFiddle 演示
Hello again and thank you in advance for your help.
I've checked a few prior questions and couldn't find this exact situation.
I'm trying to transpose/pivot a row to column, but the results are based on a date function in the where clause, making my selects somewhat variable.
SELECT DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') as Install_Date, Count(packet_details.installDate) FROM packet_details WHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY AND packet_details.installDate "*lessthan*" CURRENT_DATE + INTERVAL 7 DAY GROUP BY installDate *lessthan symbol wont show here on Stack & i don't know how to fix it
Not sure if that makes sense so I included a fiddle: http://sqlfiddle.com/#!2/5b235/3/0 So something like this:
INSTALL_DATE COUNT 1/24/2013 2 1/25/2013 2 1/26/2013 2 1/27/2013 2 1/28/2013 2 1/29/2013 1 2/3/2013 1 2/4/2013 1 2/5/2013 5 2/6/2013 4
Turned into:
INSTALL_DATE 1/24/2013 1/25/2013 1/26/2013 1/27/2013 1/28/2013.... COUNT 2 2 2 2 2 1
解决方案
SELECT Install_DATE,
MAX(CASE WHEN Install_DATE = '01-24-2013' THEN totalCount END) `01-24-2013`,
MAX(CASE WHEN Install_DATE = '01-25-2013' THEN totalCount END) `01-25-2013`,
MAX(CASE WHEN Install_DATE = '01-26-2013' THEN totalCount END) `01-26-2013`,
.......
FROM
(
SELECT DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') as Install_Date,
Count(packet_details.installDate) totalCount
FROM packet_details
WHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY AND
packet_details.installDate < CURRENT_DATE + INTERVAL 7 DAY
GROUP BY installDate
) s
- SQLFiddle Demo
For unknown number of Install_Date
, a Dynamic Query is much preferred,
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'MAX(CASE WHEN Install_DATE = ''',
Install_Date,
''' then totalCount end) AS `', Install_Date, '`' )
) INTO @sql
FROM
(
SELECT DATE_FORMAT(packet_details.installDate,'%m-%d-%Y') as Install_Date,
Count(packet_details.installDate) totalCount
FROM packet_details
WHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY AND
packet_details.installDate < CURRENT_DATE + INTERVAL 7 DAY
GROUP BY installDate
) s;
SET @sql = CONCAT('SELECT Install_DATE, ', @sql, '
FROM
(
SELECT DATE_FORMAT(packet_details.installDate,''%m-%d-%Y'') as Install_Date,
Count(packet_details.installDate) totalCount
FROM packet_details
WHERE packet_details.installDate >= CURRENT_DATE - INTERVAL 7 DAY AND
packet_details.installDate < CURRENT_DATE + INTERVAL 7 DAY
GROUP BY installDate
) s');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
- SQLFiddle Demo
相关文章