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;
相关推荐
FungLeo2 小时前
FengCMS · PHP+MySQL 升级实录 ·双数据库验证:MySQL8.4与MariaDB 10.11的兼容矩阵
数据库·mysql·php·工程实践·fengcms
XW9632584 小时前
除湿机行业组织能力与人才地图报告
大数据·服务器·网络·数据结构·数据库·人工智能
Decay__5 小时前
PostgreSQL 性能调优方法论 —— pg_stat_statements、EXPLAIN ANALYZE、瓶颈定位流程与参数基线
运维·数据库·sql·postgresql
前端兰博7 小时前
分库分表
数据库
智能收银研究所8 小时前
收银系统选型对比:美团、客如云、拉卡拉、收钱吧各适合什么门店
java·前端·数据库
有个傻瓜9 小时前
手机号明明查不到,注册却提示“已存在“:MyBatis-Plus 逻辑删除与唯一索引冲突排错复盘与五种方案实测
mysql·mybatis-plus·故障排查·唯一索引·逻辑删除
七夜zippoe9 小时前
金仓 Kingbase 安装与 Java 连接(保姆级):从装库到 JDBC 跑通全记录
java·数据库·jdbc·kingbase·金仓
FungLeo10 小时前
FengCMS · PHP+MySQL 升级实录 · 留言模块的六项问题:未审核进公开列表、旧回复覆盖、批量删除恢复
android·mysql·php·工程实践·fengcms
stormsha10 小时前
中文路径让 MySQL 起不来:Failed to find valid data directory 与 Illegal byte sequence
运维·数据库·mysql
stormsha10 小时前
MySQL 报 `data directory has files in it`:初始化失败一次,之后就永久起不来
运维·数据库·mysql