mysql行列转换方法总结
这是一道行转列并且构造交叉表的问题:
http://topic.csdn.net/u/20090530/23/0b782674-4b0b-4cf5-bc1a-e8914aaee5ab.html
数据样本:
create table tx(
id int primary key,
c1 char(2),
c2 char(2),
c3
int
);
insert
into tx values
(1 ,'A1','B1',9),
(2 ,'A2','B1',7),
(3
,'A3','B1',4),
(4 ,'A4','B1',2),
(5 ,'A1','B2',2),
(6
,'A2','B2',9),
(7 ,'A3','B2',8),
(8 ,'A4','B2',5),
(9
,'A1','B3',1),
(10 ,'A2','B3',8),
(11 ,'A3','B3',8),
(12
,'A4','B3',6),
(13 ,'A1','B4',8),
(14 ,'A2','B4',2),
(15
,'A3','B4',6),
(16 ,'A4','B4',9),
(17 ,'A1','B4',3),
(18
,'A2','B4',5),
(19 ,'A3','B4',2),
(20
,'A4','B4',5);
mysql>
select * from tx;
+----+------+------+------+
| id | c1 | c2 |
c3 |
+----+------+------+------+
| 1 | A1 | B1 | 9 |
| 2 |
A2 | B1 | 7 |
| 3 | A3 | B1 | 4 |
| 4 | A4 | B1 | 2
|
| 5 | A1 | B2 | 2 |
| 6 | A2 | B2 | 9 |
| 7 | A3 |
B2 | 8 |
| 8 | A4 | B2 | 5 |
| 9 | A1 | B3 | 1 |
|
10 | A2 | B3 | 8 |
| 11 | A3 | B3 | 8 |
| 12 | A4 |
B3 | 6 |
| 13 | A1 | B4 | 8 |
| 14 | A2 | B4 | 2 |
|
15 | A3 | B4 | 6 |
| 16 | A4 | B4 | 9 |
| 17 | A1 |
B4 | 3 |
| 18 | A2 | B4 | 5 |
| 19 | A3 | B4 | 2 |
|
20 | A4 | B4 | 5 |
+----+------+------+------+
20 rows in set (0.00
sec)
mysql>
期望结果
+------+-----+-----+-----+-----+------+
|Total |B1 |B2 |B3 |B4 |Total
|
+------+-----+-----+-----+-----+------+
|A1 |9 |2 |1 |11 |23 |
|A2 |7 |9 |8 |7 |31 |
|A3 |4 |8 |8 |8 |28 |
|A4 |2 |5 |6 |14 |27 |
|Total
|22 |24 |23 |40 |109 |
+------+-----+-----+-----+-----+------+
1. 利用SUM(IF()) 生成列 + WITH ROLLUP 生成汇总行,并利用 IFNULL将汇总行标题显示为
Total
mysql>
SELECT
IFNULL(c1,'total')
AS total,
SUM(IF(c2='B1',c3,0)) AS B1,
SUM(IF(c2='B2',c3,0)) AS
B2,
SUM(IF(c2='B3',c3,0)) AS B3,
SUM(IF(c2='B4',c3,0)) AS
B4,
SUM(IF(c2='total',c3,0)) AS total
FROM (
SELECT
c1,IFNULL(c2,'total') AS c2,SUM(c3) AS c3
FROM tx GROUP BY c1,c2
WITH
ROLLUP
HAVING c1 IS NOT NULL
) AS A
GROUP BY c1
WITH
ROLLUP;
+-------+------+------+------+------+-------+
| total | B1 | B2 |
B3 | B4 | total |
+-------+------+------+------+------+-------+
|
A1 | 9 | 2 | 1 | 11 | 23 |
| A2 | 7 | 9 | 8
| 7 | 31 |
| A3 | 4 | 8 | 8 | 8 | 28 |
|
A4 | 2 | 5 | 6 | 14 | 27 |
| total | 22 | 24 | 23
| 40 | 109 |
+-------+------+------+------+------+-------+
5 rows in
set, 1 warning (0.00 sec)
2. 利用SUM(IF()) 生成列 + UNION 生成汇总行,并利用 IFNULL将汇总行标题显示为
Total
mysql>
select
c1,
sum(if(c2='B1',C3,0)) AS B1,
sum(if(c2='B2',C3,0)) AS
B2,
sum(if(c2='B3',C3,0)) AS B3,
sum(if(c2='B4',C3,0)) AS B4,SUM(C3) AS
TOTAL
from tx
group by C1
UNION
SELECT 'TOTAL',sum(if(c2='B1',C3,0))
AS B1,
sum(if(c2='B2',C3,0)) AS B2,
sum(if(c2='B3',C3,0)) AS
B3,
sum(if(c2='B4',C3,0)) AS B4,SUM(C3) FROM TX;
+-------+------+------+------+------+-------+
|
c1 | B1 | B2 | B3 | B4 | TOTAL
|
+-------+------+------+------+------+-------+
| A1 | 9 | 2
| 1 | 11 | 23 |
| A2 | 7 | 9 | 8 | 7 | 31 |
|
A3 | 4 | 8 | 8 | 8 | 28 |
| A4 | 2 | 5 | 6
| 14 | 27 |
| TOTAL | 22 | 24 | 23 | 40 | 109
|
+-------+------+------+------+------+-------+
5 rows in set (0.00
sec)
mysql>
3. 利用SUM(IF()) 生成列,直接生成结果不再利用子查询
mysql>
select
ifnull(c1,'total'),
sum(if(c2='B1',C3,0))
AS B1,
sum(if(c2='B2',C3,0)) AS B2,
sum(if(c2='B3',C3,0)) AS
B3,
sum(if(c2='B4',C3,0)) AS B4,SUM(C3) AS TOTAL
from tx
group by C1
with rollup ;
+--------------------+------+------+------+------+-------+
|
ifnull(c1,'total') | B1 | B2 | B3 | B4 | TOTAL
|
+--------------------+------+------+------+------+-------+
|
A1 | 9 | 2 | 1 | 11 | 23 |
|
A2 | 7 | 9 | 8 | 7 | 31 |
|
A3 | 4 | 8 | 8 | 8 | 28 |
|
A4 | 2 | 5 | 6 | 14 | 27 |
|
total | 22 | 24 | 23 | 40 | 109
|
+--------------------+------+------+------+------+-------+
5 rows in set
(0.00 sec)
mysql>
4. 动态,适用于列不确定情况,
mysql>
SET @EE='';
mysql> SELECT @EE :=CONCAT(@EE
,'SUM(IF(C2=\'',C2,'\'',',C3,0)) AS ',C2,',') FROM (SELECT DISTINCT C2 FROM TX)
A;
mysql>
SET @QQ=CONCAT('SELECT ifnull(c1,\'total\'),',LEFT(@EE
,LENGTH(@EE)-1),' ,SUM(C3) AS TOTAL FROM TX GROUP BY C1 WITH ROLLUP');
Query
OK, 0 rows affected (0.00 sec)
mysql>
PREPARE stmt2 FROM @QQ ;
Query OK, 0 rows affected (0.00 sec)
Statement
prepared
mysql>
EXECUTE
stmt2;
+--------------------+------+------+------+------+-------+
|
ifnull(c1,'total') | B1 | B2 | B3 | B4 | TOTAL
|
+--------------------+------+------+------+------+-------+
|
A1 | 9 | 2 | 1 | 11 | 23 |
|
A2 | 7 | 9 | 8 | 7 | 31 |
|
A3 | 4 | 8 | 8 | 8 | 28 |
|
A4 | 2 | 5 | 6 | 14 | 27 |
|
total | 22 | 24 | 23 | 40 | 109
|
+--------------------+------+------+------+------+-------+
5 rows in set
(0.00 sec)
mysql>
其实数据库中也可以用 CASE
WHEN / DECODE 代替 IF
sum(if(c2='B1',C3,0))
AS B1
可改写为
sum(case
c2 when 'B1'
then C3 else 0 end) AS B1
mysql行列转换方法总结的更多相关文章
- 【转载】mysql行列转换方法总结
[转载]mysql行列转换方法总结 [MySQL] 行列转换变化各种方法实现总结(行变列报表统计.列变行数据记录统计等) Mysql 列转行统计查询 .行转列统计查询 在某些数据库中有交叉表,但在My ...
- 【转】mysql行列转换方法总结
转:http://blog.chinaunix.net/uid-7692530-id-2567582.html 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解 ...
- mysql 行列动态转换(列联表,交叉表)
mysql 行列动态转换(列联表,交叉表) (1)动态,适用于列不确定情况 create table table_name( id int primary key, col1 char(2), col ...
- MySQL行列转换
分类: Mysql/postgreSQL 在某些数据库中有交叉表,但在MySQL中却没有这个功能,但网上看到有不少朋友想找出一个解决方法,特发贴集思广义.http://topic.csdn.net/u ...
- 【学亮IT手记】MySql行列转换案例
create table score( name ), math int, english int ); ,); ,); ,); ,); SHOW tables; SELECT * from scor ...
- MySQL行列转换拼接
mysql> select TBL_ID,CREATE_TIME,LAST_ACCESS_TIME,TBL_NAME,TBL_TYPE from TBLS; +--------+-------- ...
- mysql行列互相转换
列转行: mysql> select * from test; +------+----------+-------+ | id | subject | score | +------+---- ...
- MySQL 行列相互转换
行列相互转换 /*创建表*/ CREATE TABLE ic ( NAME ), Product ), amount INT ); INSERT INTO ic VALUES (), (), (), ...
- Mysql 行列转换
一.第一种 原数据表 转换后 DROP TABLE IF EXISTS tempdynamic; CREATE TEMPORARY TABLE tempdynamic ( SELECT p.fsPay ...
随机推荐
- Django中使用CKEditor代码高亮显示插件Code Snippet
Django使用CKEditor可以安装django-ckeditor这个模块,具体步骤可按照这里进行:http://www.nanerbang.com/article/2/ 我在富文本编辑器中想使用 ...
- Class.getResourceAsStream()与ClassLoader.getResourceAsStream()的区别
Class.getResourceAsStream() 会指定要加载的资源路径与当前类所在包的路径一致. 例如你写了一个MyTest类在包com.test.mycode 下,那么MyTest.clas ...
- TreeSet集合如何保证元素唯一
TreeSet: 1.特点 TreeSet是用来排序的, 可以指定一个顺序, 对象存入之后会按照指定的顺序排列 2.使用方式 a.自然顺序(Comparable) TreeSet类的add()方法中会 ...
- Previous operation has not finished; run 'cleanup' if it was interrupted
在使用myeclipse的时候,点击保存的时候,控制台窗口总是弹出这个svn :Previous operation has not finished; run 'cleanup' if it was ...
- 使用Struts2校验器
今天遇到了这样的问题:我的jsp页面.web.xml.struts.xml.UserAction-validation.xml等内容写的都正确,就是无法使用校验器!在网上找了半天就是不出来,迫不得已我 ...
- Spark2.1.0分布式集群安装
一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh/p/6623530.html 1.2 Hadoop 参见博文:http://www.cnblogs ...
- 受限玻尔兹曼机(RBM)原理总结
在前面我们讲到了深度学习的两类神经网络模型的原理,第一类是前向的神经网络,即DNN和CNN.第二类是有反馈的神经网络,即RNN和LSTM.今天我们就总结下深度学习里的第三类神经网络模型:玻尔兹曼机.主 ...
- suse安装svn服务端和客户端的使用
suse安装svn服务端 一. 安装服务端 配置网络安装源(suse11sp1为例) 新建11.1.repo11.1为软件源名称,可自定义文件并添加如下内容后保存 linux-e0xg:/etc/zy ...
- Sublime Text前端开发环境配置
Sublime Text是前端开发不可不说的编辑器,本文以Sublime Text 3为例讲解一下如何搭建前端的开发环境. 下载Sublime Text 3 下载地址 #==> Sublime ...
- Java web的几种异常处理 (转)
一.在servlet容器中处理异常 以下两种方式: 1. 在web.xml定义异常处理 如果没有在web的应用中作异常处理,那么异常就会抛给Servlet容器,应该说此时Servlet容器是处理异常 ...