mysql-sql练习-5-行列互转

目录

[成绩单 简单互转](#成绩单 简单互转)

需求

[多行转多列 分组 判断 聚合](#多行转多列 分组 判断 聚合)

[理解 分组 合并](#理解 分组 合并)

[逆向需求 多列转多行 输出 合并](#逆向需求 多列转多行 输出 合并)

[abc 去重 合并 拆分](#abc 去重 合并 拆分)

需求

建表

多行转多列

[逆向需求 多列转多行](#逆向需求 多列转多行)

[拆分 按长度](#拆分 按长度)

[拆分 按个数](#拆分 按个数)


成绩单简单互转

需求

多行转多列 分组 判断 聚合

sql 复制代码
with tmp as(-- 分组,只输出语文 其他是0 ==> 条件判断,聚合
  select
    s_id,
    sum(if(c_id = '01',score,0)) '语文01',
    sum(if(c_id = '02',score,0)) '数学02',
    max(if(c_id = '03',score,0)) '英语03',
    sum(score) sum_score
  from score
  group by s_id
)
select -- 输出格式
  *,
  dense_rank() over(order by sum_score desc) dr -- 窗口范围 0-当前行
from tmp;

理解 分组 合并

sql 复制代码
select
  s_id, -- 分组:右侧括号图 多行
  group_concat(c_id) c_id, -- 多行合并成一行字符串 有空则空
  group_concat(score) score,
  group_concat(if(c_id = '01',score,0)) '语文01',
  group_concat(if(c_id = '01',score,0)) '数学02',
  group_concat(if(c_id = '01',score,0)) '英语03'
from score
group by s_id;

逆向需求 多列转多行 输出 合并

sql 复制代码
with tmp as(
  with tmp as(-- 分组:右侧括号图,只输出语文 其他是0 ==> 条件判断,聚合
    select
      s_id,
      sum(if(c_id = '01',score,0)) '语文01',
      sum(if(c_id = '02',score,0)) '数学02',
      max(if(c_id = '03',score,0)) '英语03',
      sum(score) sum_score
    from score
    group by s_id
  )
  select -- 输出格式
    *,
    dense_rank() over(order by sum_score desc) dr -- 窗口范围 0-当前行
  from tmp
)
select s_id,'01' c_id,语文01 score from tmp union -- 输出需要的列 合并
select s_id,'02' c_id,数学02 score from tmp union
select s_id,'03' c_id,英语03 score from tmp;

abc 去重 合并 拆分

需求

建表

sql 复制代码
create table abc(
	a int comment '年份',
	b varchar(2) comment '字母',
	c int comment '整数'
) comment '行列互转 合并拆分';

insert into abc
values
	('2014','A',10),
	('2014','B',9),
	('2014','B',6),
	('2015','A',8),
	('2015','B',7);
select * from abc;

多行转多列

sql 复制代码
with tmp as(-- b去重
  select
    a,b,group_concat(c) c
  from abc
  group by a,b
)
select
  a,
  sum(if(b = 'A',c,0)) col_A,
  max(if(b = 'B',c,0)) col_B  -- 字符串和整数聚合 只能max
from tmp
group by a;

逆向需求 多列转多行

拆分 按长度

sql 复制代码
with tmp as(-- 作为初始表
  with tmp as(
    select
      a,b,group_concat(c) c
    from abc
    group by a,b
  )
  select
    a,
    sum(if(b = 'A',c,0)) col_A,
    max(if(b = 'B',c,0)) col_B  -- 字符串和整数聚合 只能max
  from tmp
  group by a
)
select a,'A' b,col_A c from tmp union -- 输出需要的列 重命名
select a,'B' b,substring(col_B,1,1) c from tmp union -- 去重
select a,'B' b,substring(col_B,-1,1) c from tmp
order by a;

拆分 按个数

sql 复制代码
with tmp as(-- 作为初始表
  with tmp as(
    select
      a,b,group_concat(c) c
    from abc
    group by a,b
  )
  select
    a,
    sum(if(b = 'A',c,0)) col_A,
    max(if(b = 'B',c,0)) col_B  -- 字符串和整数聚合 只能max
  from tmp
  group by a
)
select a,'A' b,col_A c from tmp union
select a,'B' b,substring_index(col_B,',',1) c from tmp union
select a,'B' b,substring_index(col_B,',',-1) c from tmp
order by a;
相关推荐
小宇成长录8 分钟前
Mysql:数据库和表增删查改基本语句
数据库·mysql·数据库备份
团儿.1 小时前
解锁MySQL高可用新境界:深入探索MHA架构的无限魅力与实战部署
数据库·mysql·架构·mysql之mha架构
程序猿小D1 小时前
第二百六十七节 JPA教程 - JPA查询AND条件示例
java·开发语言·前端·数据库·windows·python·jpa
权^2 小时前
MySQL--聚合查询、联合查询、子查询、合并查询(上万字超详解!!!)
大数据·数据库·学习·mysql
Code成立3 小时前
1、深入理解Redis线程模型
数据库·redis·bootstrap
缘友一世4 小时前
macos安装mongodb
数据库·mongodb·macos
万事大吉CC6 小时前
mysql单表查询·3
数据库·mysql
bin91536 小时前
【EXCEL数据处理】000010 案列 EXCEL文本型和常规型转换。使用的软件是微软的Excel操作的。处理数据的目的是让数据更直观的显示出来,方便查看。
大数据·数据库·信息可视化·数据挖掘·数据分析·excel·数据可视化
Miqiuha7 小时前
lock_guard和unique_lock学习总结
java·数据库·学习
一 乐8 小时前
学籍管理平台|在线学籍管理平台系统|基于Springboot+VUE的在线学籍管理平台系统设计与实现(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习