列转行-多列转多行(横表变竖表)

一、基础数据

有学生成绩表,包含学生id、语文、数学、英语三科成绩

+-------------+--------+---------+---------+
| student_id  | yuwen  | shuxue  | yingyu  |
+-------------+--------+---------+---------+
| 001         | 89     | 95      | 77      |
| 002         | 92     | 83      | 97      |
| 003         | 81     | 94      | 88      |
+-------------+--------+---------+---------+

二、函数介绍

三、多列转多行(横表变竖表)

原始数据为一张横表,分别有三列成绩列,想要转成竖表,需要转换成三列分别为 学生id、学科、成绩,转换完成之后学生id将不再是主键。

期望结果

+-------------+----------+--------+
| student_id  | subject  | score  |
+-------------+----------+--------+
| 001         | 语文       | 89     |
| 001         | 数学       | 95     |
| 001         | 英语       | 77     |
| 002         | 语文       | 92     |
| 002         | 数学       | 83     |
| 002         | 英语       | 97     |
| 003         | 语文       | 81     |
| 003         | 数学       | 94     |
| 003         | 英语       | 88     |
+-------------+----------+--------+

1.union all 完成数据

使用union all 对不同学科的数据进行组合,得到最终结果。

执行SQL

--语文成绩
select student_id,
       '语文' as subject,
       yuwen  as score
from t_student_score_02
union all
--数学成绩
select student_id,
       '数学' as subject,
       shuxue as score
from t_student_score_02
union all
--英语成绩
select student_id,
       '英语' as subject,
       yingyu as score
from t_student_score_02

执行结果

+-------------+----------+--------+
| student_id  | subject  | score  |
+-------------+----------+--------+
| 001         | 语文       | 89     |
| 002         | 语文       | 92     |
| 003         | 语文       | 81     |
| 001         | 数学       | 95     |
| 002         | 数学       | 83     |
| 003         | 数学       | 94     |
| 001         | 英语       | 77     |
| 002         | 英语       | 97     |
| 003         | 英语       | 88     |
+-------------+----------+--------+

2.数据拼接后炸裂开

2.1拼接数据

使用concat对科目和科目对应的分数进行拼接,然后使用concat_ws把不同科目数据拼接到一起

执行SQL

select student_id,
       concat_ws(',', concat('语文:', yuwen), concat('数学:', shuxue), concat('英语:', yingyu)) as sub_scores
from t_student_score_02

执行结果

+-------------+--------------------+
| student_id  |     sub_scores     |
+-------------+--------------------+
| 001         | 语文:89,数学:95,英语:77  |
| 002         | 语文:92,数学:83,英语:97  |
| 003         | 语文:81,数学:94,英语:88  |
+-------------+--------------------+
2.2 lateral view explode 将成绩列转行

使用lateral view explode 将成绩列转行,然后使用split将科目和分数分开。
执行SQL

select student_id,
       split(sub_score, ':')[0] as subject,
       split(sub_score, ':')[1] as score
from (select student_id,
             concat_ws(',', concat('语文:', yuwen), concat('数学:', shuxue), concat('英语:', yingyu)) as sub_scores
      from t_student_score_02)
         lateral view explode(split(sub_scores, ',')) t as sub_score

执行结果

+-------------+----------+--------+
| student_id  | subject  | score  |
+-------------+----------+--------+
| 001         | 语文       | 89     |
| 001         | 数学       | 95     |
| 001         | 英语       | 77     |
| 002         | 语文       | 92     |
| 002         | 数学       | 83     |
| 002         | 英语       | 97     |
| 003         | 语文       | 81     |
| 003         | 数学       | 94     |
| 003         | 英语       | 88     |
+-------------+----------+--------+

四、数据准备

--建表语句
CREATE TABLE IF NOT EXISTS t_student_score_02
(
    student_id string, -- 学生id
    yuwen      bigint,--语文成绩
    shuxue     bigint, --数学成绩
    yingyu     bigint  --英语成绩
)
    COMMENT '学生成绩表';
--数据插入语句
insert into t_student_score_02
select student_id,
       sum(case when subject = '语文' then score end) as yuwen,
       sum(case when subject = '数学' then score end) as shuxue,
       sum(case when subject = '英语' then score end) as yingyu
from t_student_score
group by student_id

相关推荐

  1. 行转列-collect_list,collect_set进行简单行转列
  2. 行转列-使用transform进行有序行转列
  3. 行转列-使用transform进行有序行转列-多列一一对应
  4. 行转列-多行转多列(竖表转横表)
  5. 列转行-多列转多行(横表变竖表)
  6. 列转行-lateral view explode列转行
  7. 列转行-explode_outer和lateral view outer
  8. 列转行-posexplode多列对应转行
  9. 列转行-lateral view outer posexplode及posexplode_outer多列对应转行
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值