当前位置:网站首页>SQL行转列、列转行
SQL行转列、列转行
2022-07-21 15:52:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
SQL行转列、列转行
这个主题还是比较常见的,行转列主要适用于对数据作聚合统计,如统计某类目的商品在某个时间区间的销售情况。列转行问题同样也很常见。
一、整理测试数据
create table wyc_test(
id int(32) not null auto_increment,
name varchar(80) default null,
date date default null,
scount int(32),
primary key (id)
);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (1,'小说','2013-09-01',10000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (2,'微信','2013-09-01',20000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (3,'小说','2013-09-02',30000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (4,'微信','2013-09-02',35000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (5,'小说','2013-09-03',31000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (6,'微信','2013-09-03',36000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (7,'小说','2013-09-04',35000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (8,'微信','2013-09-04',38000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (9,'小说','2013-09-01',80000);
INSERT INTO `wyc_test` (`id`,`name`,`date`,`scount`) VALUES (10,'微信','2013-09-01',70000);
二、行转列
主要思路是分组后使用case进行条件判断处理
#行转列
select
a.date,
sum(case a.name
when '小说' then a.scount
else 0
end) 'sum_小说',
max(case a.name
when '小说' then a.scount
else 0
end) 'max_小说',
sum(case a.name
when '微信' then a.scount
else 0
end) '微信',
max(case a.name
when '小说' then a.scount
else 0
end) 'max_微信'
from
wyc_test a
group by date;
结果:
三、列转行
主要思路也是分组后使用case
#列转行
select
a.date,
concat('小说:',
cast(sum(case a.name
when '小说' then a.scount
else 0
end)
as char),
'微信',
cast(sum(case a.name
when '微信' then a.scount
else 0
end)
as char)) as 'str'
from
wyc_test a
group by a.date;
#列转行
#1.使用mysql提供的函数分组
select a.date,group_concat(a.name,'总量:', a.scount) from wyc_test a group by a.date,a.name;
#2.使用mysql提供的函数分组
select a.date,a.name, group_concat(a.name, '总量:', a.scount) from wyc_test a group by a.date,a.name;
#3.普通group结合字符串拼接
SELECT
a.date,
concat('小说总量:',
cast(sum(case a.name
when '小说' then a.scount
else 0
end)
as char)) as '小说',
concat('微信总量:',
cast(sum(case a.name
when '微信' then a.scount
else 0
end)
as char)) as '微信'
from
wyc_test a
group by a.date;
结果:
四、列转行详解 1.1、初始测试数据 表结构:TEST_TB_GRADE2 Sql代码 create table TEST_TB_GRADE2 ( ID NUMBER(10) not null, USER_NAME VARCHAR2(20 CHAR), CN_SCORE FLOAT, MATH_SCORE FLOAT, EN_SCORE FLOAT ) 初始数据如下图:
1.2、 如果需要实现如下的查询效果图:
这就是最常见的列转行,主要原理是利用SQL里面的union,具体的sql语句如下: Sql代码 select user_name, ‘语文’ COURSE , CN_SCORE as SCORE from test_tb_grade2 union select user_name, ‘数学’ COURSE, MATH_SCORE as SCORE from test_tb_grade2 union select user_name, ‘英语’ COURSE, EN_SCORE as SCORE from test_tb_grade2 order by user_name,COURSE 也可以利用【 insert all into … select 】来实现,首先需要先建一个表TEST_TB_GRADE3: Sql代码 create table TEST_TB_GRADE3 ( USER_NAME VARCHAR2(20 CHAR), COURSE VARCHAR2(20 CHAR), SCORE FLOAT ) 再执行下面的sql: Sql代码 insert all into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, ‘语文’, CN_SCORE) into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, ‘数学’, MATH_SCORE) into test_tb_grade3(USER_NAME,COURSE,SCORE) values(user_name, ‘英语’, EN_SCORE) select user_name, CN_SCORE, MATH_SCORE, EN_SCORE from test_tb_grade2; commit;
别忘记commit操作,然后再查询TEST_TB_GRADE3,发现表中的数据就是列转成行了。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/124752.html原文链接:https://javaforall.cn
边栏推荐
- 如何编写sql语句?
- 2022/7/18 cf训练
- 31下一个排列
- Area line chart of data visualization chart
- Dynamics 365: 查找哪些记录使用Access Team这种方式分享给了某一个User
- TMECH发表优必选运控技术最新进展:实现人形机器人高鲁棒性行走
- Dynamics crm: use setfilterxml to filter the records that need to be displayed in the subgrid control of the form
- Keras deep learning practice (12) -- facial feature point detection
- Dynamics CRM: 如何监控和管理Workflow process,并查看它们的运行历史
- “新能源+储能“从数字孪生开始,图扑将智慧电力做到极致
猜你喜欢
Error 1053: The service did not respond to the start or control request in a timely fashion.问题排查
TCL的折叠屏故事,资本市场会看好吗?
Dynamics 365: 详解虚实体(Virtual Entity) 从0到1
《通信软件开发与应用》课程结业报告
Keras深度学习实战(11)——可视化神经网络中间层输出
Keras深度学习实战(10)——迁移学习
Notes on the establishment of the official website of the public network (V): the complete process of filing the domain name with the Ministry of industry and information technology and resolving the
20、shell编程之变量
电脑上如何开启多个微信,微信多开
22括号生成
随机推荐
真人踩过的坑,告诉你避免自动化测试新手常犯的10个错误
124二叉树中的最大路径和
MYSQL8 遇到的服务启动后停止的问题
Dynamics CRM: 查询字符串参数(Query String Parameters)的使用方法
Special analysis of China's third-party payment market in 2022
Dynamics 365: how to remove the restriction that an entity can only create two access team templates
天正T20 V8.0全套软件安装包下载及安装教程
2022/7/ 20 训练记录
Detailed explanation of redis [data operation, persistence, jedis, cache processing] of NoSQL database
Common questions of testers during interview
电脑上如何开启多个微信,微信多开
Dynamics 365: Access Team Templates的用法
pg_wal目录下面的文件能恢复数据库吗?
Azure Key Vault(3):Key和Secret的区别
Dynamics 365: 推荐几个XrmToolBox中创建ER(Entity Relationship)图的插件
感觉的定义
无偿分享 简单易用 贝茨训练法 调解睫状肌 近视恢复训练
mysql中的字段如何选择合适的数据类型呢?
Area line chart of data visualization chart
Web3, encrypted VC new air outlet