关于优雅的使用SQL多行转多列的记录(doris)

文章目录

  • 应用需求场景
  • 记录过程
      • [1. 准备数据](#1. 准备数据)
      • [2. 给数据根据姓名分组,加上序号.](#2. 给数据根据姓名分组,加上序号.)
      • [3. 根据name分组成map结构](#3. 根据name分组成map结构)
      • [4. 拆分map](#4. 拆分map)

应用需求场景

准备的数据是这样的:

需要将每个人的成绩显示在一行上,需要的结果如下,但是我的情况是课程有非常多,但不是每个课程都存在。我只想把这个人学习的3个课程列出来,所以这类有个问题,就是每个同学的课程不一定是语文、数学、英语,但是最大不多于3门课。

最终的结果如下:

记录过程

1. 准备数据

sql 复制代码
with tmp_a as (
	select 'a' name,'语文' clazz, 80 as score
	union all
	select 'a' name,'高数' clazz, 85 as score
	union all
	select 'a' name,'英语' clazz, 20 as score
	union all
	select 'b' name,'微积分' clazz, 70 as score
	union all
	select 'b' name,'数学' clazz, 75 as score
	union all
	select 'b' name,'现代' clazz, 71 as score
	union all
	select 'c' name,'物理' clazz, 70 as score
	union all
	select 'c' name,'音乐' clazz, 75 as score
	union all
	select 'c' name,'体育' clazz, 71 as score
)
select * from tmp_a;

2. 给数据根据姓名分组,加上序号.

sql 复制代码
with tmp_a as (
	select 'a' name,'语文' clazz, 80 as score
	union all
	select 'a' name,'高数' clazz, 85 as score
	union all
	select 'a' name,'英语' clazz, 20 as score
	union all
	select 'b' name,'微积分' clazz, 70 as score
	union all
	select 'b' name,'数学' clazz, 75 as score
	union all
	select 'b' name,'现代' clazz, 71 as score
	union all
	select 'c' name,'物理' clazz, 70 as score
	union all
	select 'c' name,'音乐' clazz, 75 as score
	union all
	select 'c' name,'体育' clazz, 71 as score
)
select *,row_number() over(partition by name order by clazz) rk
from tmp_a;

3. 根据name分组成map结构

注意:这里用到的map_agg是doris里面的,其他数据库不确定有没有。

map_agg(key,value) 这样使用的,这里将序号作为key,后面方便统一取值。

sql 复制代码
with tmp_a as (
	select 'a' name,'语文' clazz, 80 as score
	union all
	select 'a' name,'高数' clazz, 85 as score
	union all
	select 'a' name,'英语' clazz, 20 as score
	union all
	select 'b' name,'微积分' clazz, 70 as score
	union all
	select 'b' name,'数学' clazz, 75 as score
	union all
	select 'b' name,'现代' clazz, 71 as score
	union all
	select 'c' name,'物理' clazz, 70 as score
	union all
	select 'c' name,'音乐' clazz, 75 as score
	union all
	select 'c' name,'体育' clazz, 71 as score
),
tmp_b as (
select *,row_number() over(partition by name order by clazz) rk
from tmp_a
)
select name,
map_agg(rk,clazz) clazz,
map_agg(rk,score) score
from tmp_b group by name;

4. 拆分map

这里必须写死,没其他好方法,而且列是固定写死的。

clazz[1] 这里的1就是上面的放进去的rk的值,由于我们每个人最多只有3门课,

所以可以就取 clazz[1] clazz[2] clazz[3]

sql 复制代码
with tmp_a as (
	select 'a' name,'语文' clazz, 80 as score
	union all
	select 'a' name,'高数' clazz, 85 as score
	union all
	select 'a' name,'英语' clazz, 20 as score
	union all
	select 'b' name,'微积分' clazz, 70 as score
	union all
	select 'b' name,'数学' clazz, 75 as score
	union all
	select 'b' name,'现代' clazz, 71 as score
	union all
	select 'c' name,'物理' clazz, 70 as score
	union all
	select 'c' name,'音乐' clazz, 75 as score
	union all
	select 'c' name,'体育' clazz, 71 as score
),
tmp_b as (
select *,row_number() over(partition by name order by clazz) rk
from tmp_a
), 
tmp_c as (
select name,
map_agg(rk,clazz) clazz,
map_agg(rk,score) score
from tmp_b group by name
)
select name,
clazz[1] clazz_1,score[1] score_1,
clazz[2] clazz_2,score[2] score_2,
clazz[3] clazz_3,score[3] score_3
from tmp_c

最终显示如下,中午没睡觉想了一个中午,脑壳痛,看起来好像也不太复杂,

如果每个人的课程增加时,只需要在最后添加一行即可。

相关推荐
RestCloud14 小时前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术17 小时前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug21 小时前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom21 小时前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*21 小时前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud
Slaughter信仰21 小时前
深入理解Java虚拟机:JVM高级特性与最佳实践(第3版)第十章知识点问答(10题)
java·jvm·数据库
麦兜*21 小时前
MongoDB 在物联网(IoT)中的应用:海量时序数据处理方案
java·数据库·spring boot·物联网·mongodb·spring
-Xie-1 天前
Mysql杂志(十六)——缓存池
数据库·mysql·缓存