mysql并行查询语句
简介: 在刚刚结束的乌镇世界互联网大会上,阿里云自研POLARDB云原生数据库当选世界互联网领先科技成果。POLARDB既融合了商业数据库稳定可靠、高性能、可扩展的特征,又具有开源云数据库简单开放、自...
2024.11.22今天主要做一个实验,先理解下mysql行列转换,后面在做一下反向的行列转换。
需求二维关系转换为三维关系。
1、环境准备create database test;use test;create table t_score(id int primary key auto_increment,name varchar(20) not null, #名字Subject varchar(10) not null, #科目Fraction double default 0 #分数);insert INTO `t_score`(name,Subject,Fraction) VALUES (‘王海‘, ‘语文‘, 86), (‘王海‘, ‘数学‘, 83), (‘王海‘, ‘英语‘, 93), (‘陶俊‘, ‘语文‘, 88), (‘陶俊‘, ‘数学‘, 84), (‘陶俊‘, ‘英语‘, 94), (‘刘可‘, ‘语文‘, 80), (‘刘可‘, ‘数学‘, 86), (‘刘可‘, ‘英语‘, 88), (‘李春‘, ‘语文‘, 89), (‘李春‘, ‘数学‘, 80), (‘李春‘, ‘英语‘, 87);2、实现方案一--使用ifselect name as 名字 ,sum(if(Subject=‘语文‘,Fraction,0)) as 语文,sum(if(Subject=‘数学‘,Fraction,0))as 数学, sum(if(Subject=‘英语‘,Fraction,0))as 英语,round(AVG(Fraction),2) as 平均分,SUM(Fraction) as 总分from t_score group by name unionselect name as 名字 , sum(语文) Chinese,sum(数学) Math,sum(英语) English,round(AVG(总分),2)as 平均分,sum(总分) score from(select ‘TOTAL‘ as name,sum(if(Subject=‘语文‘,Fraction,0)) as 语文,sum(if(Subject=‘数学‘,Fraction,0))as 数学, sum(if(Subject=‘英语‘,Fraction,0))as 英语,SUM(Fraction) as 总分from t_score group by Subject )t3、实现方案二--使用caseselect name as Name,sum(case when Subject = ‘语文‘ then Fraction end) as Chinese,sum(case when Subject = ‘数学‘ then Fraction end) as Math,sum(case when Subject = ‘英语‘ then Fraction end) as English,round(AVG(Fraction),2) as 平均分,sum(fraction)as scorefrom t_score group by nameUNION ALLselect name as Name,sum(Chinese) as Chinese,sum(Math) as Math,sum(English) as English,sum(score) as score from(select ‘TOTAL‘ as name,sum(case when Subject = ‘语文‘ then Fraction end) as Chinese,sum(case when Subject = ‘数学‘ then Fraction end) as Math,sum(case when Subject = ‘英语‘ then Fraction end) as English,sum(fraction)as scorefrom t_score group by Subject)t;4、实现方案三--使用maxselect name as Name,max(case when Subject = ‘语文‘ then Fraction end) as Chinese,max(case when Subject = ‘数学‘ then Fraction end) as Math,max(case when Subject = ‘英语‘ then Fraction end) as English,sum(fraction)as scorefrom t_score group by nameUNION ALLselect name as Name,sum(Chinese) as Chinese,sum(Math) as Math,sum(English) as English,sum(score) as score from(select ‘TOTAL‘ as name,max(case when Subject = ‘语文‘ then Fraction end) as Chinese,max(case when Subject = ‘数学‘ then Fraction end) as Math,max(case when Subject = ‘英语‘ then Fraction end) as English,sum(fraction)as scorefrom t_score group by Subject)t;5、实现方案四--with rollupgroup by 后可以跟with rollup,表示在进行分组统计的基础上再次进行汇总统计(在每个分组下都会有统计汇总)
select ifnull(name,‘TOTAL‘) name,sum(if(Subject=‘语文‘,Fraction,0)) as 语文,sum(if(Subject=‘英语‘,Fraction,0)) as 英语,sum(if(Subject=‘数学‘,Fraction,0))as 数学,sum(Fraction) 总分from t_score group by name with rollup;create table t_all as select coalesce(name,‘TOTAL‘) name,sum(if(Subject=‘语文‘,Fraction,0)) as 语文,sum(if(Subject=‘英语‘,Fraction,0)) as 英语,sum(if(Subject=‘数学‘,Fraction,0))as 数学,sum(Fraction) 总分from t_score group by name with rollup;6、反向行列转换有时我们业务部门会给我们一张excel表,一般都是三维关系的,如果想要导进数据库变成两张二维关系的表去join,应该怎么实现呢?类似:
实现方案:最简单的union
select Name as name,‘语文‘ as Subject,Chinese as Farction from t_all where Name!=‘TOTAL‘union select Name as name,‘数学‘ as Subject,Math as Farction from t_all where Name!=‘TOTAL‘union select Name as name,‘英语‘ as Subject,English as Farction from t_all where Name!=‘TOTAL‘建议大家抽空可以做一下,这个还是很有用的。觉得有用的朋友多帮忙转发哦!后面会分享更多devops和DBA方面的内容,感兴趣的朋友可以关注下~
简介: 在刚刚结束的乌镇世界互联网大会上,阿里云自研POLARDB云原生数据库当选世界互联网领先科技成果。POLARDB既融合了商业数据库稳定可靠、高性能、可扩展的特征,又具有开源云数据库简单开放、自...
2024.11.22简单数据查询操作增删改查是数据表操作的重要组成部分,尤其是数据表的查询更是数据库与各类应用交互的频繁操作之一。本文课主要介绍简单数据查询语句。查询语句基本语法查询语句是实现数据查询的SQL语句,用于实...
2024.11.21将一个数据表行列转置,有几种方法?有很多,不过今天我只介绍其中最常用的 3 种。案例:将下图 1 中的 B 列行列转置,效果如下图 2 所示。解决方案 1:复制粘贴1. 选中 B1:B6 区域 -->...
2024.11.22开发同学让我帮忙看看一个数据怎么录合适。原始的数据如下,要录入到数据库里。先取消Excel的单元格合并.删除第一行和第二行的数据,这些都是标题.然后导出CSV文件,使用文本文件编辑器打开CSV,替换 ...
2024.11.20作者:杨文DBA,负责客户项目的需求与维护,会点数据库,不限于MySQL、Redis、Cassandra、GreenPlum、ClickHouse、Elastic、TDSQL等等。本文来源:原创投稿*...
2024.11.22