提问人:peku 提问时间:10/6/2011 最后编辑:Rick Jamespeku 更新时间:8/28/2023 访问量:471319
如何在MySQL中返回数据透视表输出?
How can I return pivot table output in MySQL?
问:
如果我有一个MySQL表,如下所示:
company_name action pagecount ------------------------------- Company A PRINT 3 Company A PRINT 2 Company A PRINT 3 Company B EMAIL Company B PRINT 2 Company B PRINT 2 Company B PRINT 1 Company A PRINT 3
是否可以运行MySQL查询来获得如下输出:
company_name EMAIL PRINT 1 pages PRINT 2 pages PRINT 3 pages ------------------------------------------------------------- CompanyA 0 0 1 3 CompanyB 1 1 2 0
这个想法是可以变化的,因此输出列数量应该反映这一点,每/对一列,然后是每 .我不确定这是否被称为数据透视表,但有人建议这样做?pagecount
action
pagecount
company_name
答:
这基本上是一个数据透视表。
可以在此处找到有关如何实现此目的的出色教程: http://www.artfulsoftware.com/infotree/qrytip.php?id=78
我建议您阅读这篇文章并根据您的需求调整此解决方案。
更新
在上面的链接目前不再可用之后,我觉得有义务为在这里搜索mysql枢轴答案的所有人提供一些额外的信息。它确实有大量的信息,我不会把那里的所有东西都放在这里(甚至更多,因为我只是不想复制他们丰富的知识),但我会给出一些关于如何处理数据透视表的建议,通常以 peku 的例子为例,他首先提出了这个问题。
也许链接很快就会回来,我会密切关注它。
电子表格方式...
为此,许多人只是使用MSExcel,OpenOffice或其他电子表格工具等工具。这是一个有效的解决方案,只需将数据复制到那里并使用 GUI 提供的工具来解决此问题。
但。。。这不是问题,它甚至可能导致一些缺点,例如如何将数据放入电子表格、有问题的缩放等。
SQL方式...
鉴于他的表格看起来像这样:
CREATE TABLE `test_pivot` (
`pid` bigint(20) NOT NULL AUTO_INCREMENT,
`company_name` varchar(32) DEFAULT NULL,
`action` varchar(16) DEFAULT NULL,
`pagecount` bigint(20) DEFAULT NULL,
PRIMARY KEY (`pid`)
) ENGINE=MyISAM;
现在看看他/她想要的表:
company_name EMAIL PRINT 1 pages PRINT 2 pages PRINT 3 pages
-------------------------------------------------------------
CompanyA 0 0 1 3
CompanyB 1 1 2 0
行 (, ) 类似于条件。主要分组是 。EMAIL
PRINT x pages
company_name
为了设置条件,这更像是使用 CASE
语句。为了按某物分组,好吧,使用......GROUP BY
提供此透视的基本 SQL 可能如下所示:
SELECT P.`company_name`,
COUNT(
CASE
WHEN P.`action`='EMAIL'
THEN 1
ELSE NULL
END
) AS 'EMAIL',
COUNT(
CASE
WHEN P.`action`='PRINT' AND P.`pagecount` = '1'
THEN P.`pagecount`
ELSE NULL
END
) AS 'PRINT 1 pages',
COUNT(
CASE
WHEN P.`action`='PRINT' AND P.`pagecount` = '2'
THEN P.`pagecount`
ELSE NULL
END
) AS 'PRINT 2 pages',
COUNT(
CASE
WHEN P.`action`='PRINT' AND P.`pagecount` = '3'
THEN P.`pagecount`
ELSE NULL
END
) AS 'PRINT 3 pages'
FROM test_pivot P
GROUP BY P.`company_name`;
这应该可以非常快速地提供所需的结果。这种方法的主要缺点是,数据透视表中的行数越多,需要在 SQL 语句中定义的条件就越多。
这也可以处理,因此人们倾向于使用准备好的语句、例程、计数器等。
有关此主题的一些其他链接:
- http://anothermysqldba.blogspot.de/2013/06/pivot-tables-example-in-mysql.html
- http://www.codeproject.com/Articles/363339/Cross-Tabulation-Pivot-Tables-with-MySQL
- http://datacharmer.org/downloads/pivot_tables_mysql_5.pdf
- https://codingsight.com/pivot-tables-in-mysql/
评论
我的解决方案是在没有任何透视的 T-SQL 中:
SELECT
CompanyName,
SUM(CASE WHEN (action='EMAIL') THEN 1 ELSE 0 END) AS Email,
SUM(CASE WHEN (action='PRINT' AND pagecount=1) THEN 1 ELSE 0 END) AS Print1Pages,
SUM(CASE WHEN (action='PRINT' AND pagecount=2) THEN 1 ELSE 0 END) AS Print2Pages,
SUM(CASE WHEN (action='PRINT' AND pagecount=3) THEN 1 ELSE 0 END) AS Print3Pages
FROM
Company
GROUP BY
CompanyName
评论
SUM()
MAX()
SUM(CASE WHEN (action='PRINT' AND pagecount=1) THEN 1 ELSE 0 END)
SUM(action='PRINT' AND pagecount=1)
1
0
select t3.name, sum(t3.prod_A) as Prod_A, sum(t3.prod_B) as Prod_B, sum(t3.prod_C) as Prod_C, sum(t3.prod_D) as Prod_D, sum(t3.prod_E) as Prod_E
from
(select t2.name as name,
case when t2.prodid = 1 then t2.counts
else 0 end prod_A,
case when t2.prodid = 2 then t2.counts
else 0 end prod_B,
case when t2.prodid = 3 then t2.counts
else 0 end prod_C,
case when t2.prodid = 4 then t2.counts
else 0 end prod_D,
case when t2.prodid = "5" then t2.counts
else 0 end prod_E
from
(SELECT partners.name as name, sales.products_id as prodid, count(products.name) as counts
FROM test.sales left outer join test.partners on sales.partners_id = partners.id
left outer join test.products on sales.products_id = products.id
where sales.partners_id = partners.id and sales.products_id = products.id group by partners.name, prodid) t2) t3
group by t3.name ;
对于MySQL,您可以直接在函数中放置条件,它将被评估为布尔值0或
1
,因此您可以根据自己的条件进行计数,而无需使用语句SUM()
IF/CASE
SELECT
company_name,
SUM(action = 'EMAIL')AS Email,
SUM(action = 'PRINT' AND pagecount = 1)AS Print1Pages,
SUM(action = 'PRINT' AND pagecount = 2)AS Print2Pages,
SUM(action = 'PRINT' AND pagecount = 3)AS Print3Pages
FROM t
GROUP BY company_name
DEMO
评论
对于动态透视,请与 一起使用。
GROUP_CONCAT 函数将组中的字符串连接成一个具有各种选项的字符串。GROUP_CONCAT
CONCAT
SET @sql = NULL;
SELECT
GROUP_CONCAT(DISTINCT
CONCAT(
'SUM(CASE WHEN action = "',
action,'" AND ',
(CASE WHEN pagecount IS NOT NULL
THEN CONCAT("pagecount = ",pagecount)
ELSE pagecount IS NULL END),
' THEN 1 ELSE 0 end) AS ',
action, IFNULL(pagecount,'')
)
)
INTO @sql
FROM
t;
SET @sql = CONCAT('SELECT company_name, ', @sql, '
FROM t
GROUP BY company_name');
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
评论
使用布尔逻辑的标准 SQL 版本:
SELECT company_name
, COUNT(action = 'EMAIL' OR NULL) AS "Email"
, COUNT(action = 'PRINT' AND pagecount = 1 OR NULL) AS "Print 1 pages"
, COUNT(action = 'PRINT' AND pagecount = 2 OR NULL) AS "Print 2 pages"
, COUNT(action = 'PRINT' AND pagecount = 3 OR NULL) AS "Print 3 pages"
FROM tbl
GROUP BY company_name;
如何?
TRUE OR NULL
收益 率。
收益 率。
收益 率。
而 COUNT
只计算非 null 值。瞧。TRUE
FALSE OR NULL
NULL
NULL OR NULL
NULL
评论
有一个叫做MySQL数据透视表生成器的工具,它可以帮助您创建一个基于Web的数据透视表,您可以稍后将其导出到excel(如果您愿意)。如果数据位于单个表或多个表中,则可以工作。
您需要做的就是指定列的数据源(它支持动态列)、行、表正文中的值和表关系(如果有的话)
此工具的主页 https://mysqlreports.com/mysql-reporting-tools/mysql-pivot-table/
正确答案是:
select table_record_id,
group_concat(if(value_name='note', value_text, NULL)) as note
,group_concat(if(value_name='hire_date', value_text, NULL)) as hire_date
,group_concat(if(value_name='termination_date', value_text, NULL)) as termination_date
,group_concat(if(value_name='department', value_text, NULL)) as department
,group_concat(if(value_name='reporting_to', value_text, NULL)) as reporting_to
,group_concat(if(value_name='shift_start_time', value_text, NULL)) as shift_start_time
,group_concat(if(value_name='shift_end_time', value_text, NULL)) as shift_end_time
from other_value
where table_name = 'employee'
and is_active = 'y'
and is_deleted = 'n'
GROUP BY table_record_id
评论
other_value
SET
一种选择是结合使用 CASE。WHEN
语句在MySQL数据库的聚合中是冗余的,并动态考虑所需的查询生成以及获取结果集的正确列标题,如以下代码块所示:
SET @sql = NULL;
SELECT GROUP_CONCAT(
CONCAT('SUM( `action` = ''', action, '''',pc0,' ) AS ',action,pc1)
)
INTO @sql
FROM
(
SELECT DISTINCT `action`,
IF(`pagecount` IS NULL,'',CONCAT('page',`pagecount`)) AS pc1,
IF(`pagecount` IS NULL,'',CONCAT(' AND `pagecount` = ', pagecount, '')) AS pc0
FROM `tab`
ORDER BY CONCAT(action,pc0)
) t;
SET @sql = CONCAT('SELECT company_name,',@sql,' FROM `tab` GROUP BY company_name');
SELECT @sql;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SELECT company_name, SUM(CASE WHEN ACTION = 'Email' THEN 1 ELSE 0 END) AS "Email",
SUM(CASE WHEN ACTION = 'Print' AND pagecount = 1 THEN 1 ELSE 0 END) AS "print 1 PAGE",
SUM(CASE WHEN ACTION = 'Print' AND pagecount = 2 THEN 1 ELSE 0 END) AS "print 2 PAGE",
SUM(CASE WHEN ACTION = 'Print' AND pagecount = 3 THEN 1 ELSE 0 END) AS "print 2 PAGE"
FROM test1 GROUP BY company_name;
评论