关于优雅的使用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

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

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

相关推荐
f***01931 分钟前
【MySQL】JDBC的连接
数据库·mysql
5***T4482 分钟前
开启mysql的binlog日志
数据库·mysql
q***333720 分钟前
UNION 和 UNION ALL 的区别:深入解析 SQL 中的合并操作
数据库·sql·oracle
郑重其事,鹏程万里23 分钟前
关系型数据库(derby)
数据库
Elastic 中国社区官方博客25 分钟前
使用 A2A 协议和 MCP 在 Elasticsearch 中创建一个 LLM agent 新闻室:第二部分
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
秋邱34 分钟前
价值升维!公益赋能 + 绿色技术 + 终身学习,构建可持续教育 AI 生态
网络·数据库·人工智能·redis·python·学习·docker
郑重其事,鹏程万里42 分钟前
关系型数据库(h2)
数据库
空空kkk1 小时前
MyBatis——代理Dao方式的增删改查操作
java·数据库·mybatis
4***571 小时前
数据库(MySQL):使用命令从零开始在Navicat创建一个数据库及其数据表(一).创建基础表
数据库·mysql·oracle
啊吧怪不啊吧1 小时前
SQL之表的字符串内置函数详解
大数据·数据库·sql