多行转多列

一、基础数据

有学生成绩表,包含学生id、学科、成绩

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

二、函数介绍

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

原始数据中是一个竖表,每个学生的每个学科一行数据,对其转换成一张横表,即表中学生id为主键,包含语文、数学、英语三列,列值为对应学科分数。

期望结果

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

1.生成三个科目的成绩列

使用case when语句,对三个科目依次判断,是对应科目的取对应科目成绩,不是对应科目不取值。

执行SQL

select student_id,
       case when subject = '语文' then score end as yuwen,
       case when subject = '数学' then score end as shuxue,
       case when subject = '英语' then score end as yingyu
from t_student_score

执行结果

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

2.聚合,将每个学生的科目放到同一行

使用聚合函数,将学生不同科目成绩压缩到同一行。

执行SQL

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

执行结果

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

四、数据准备

--建表语句
CREATE TABLE IF NOT EXISTS t_student_score
(
    student_id string, -- 学生id
    subject    string, -- 学科
    score      bigint  -- 分数
)
    COMMENT '学生成绩表';

insert into t_student_score
values ('001', '语文', 89),
       ('001', '数学', 95),
       ('001', '英语', 77),
       ('002', '语文', 92),
       ('002', '数学', 83),
       ('002', '英语', 97),
       ('003', '语文', 81),
       ('003', '数学', 94),
       ('003', '英语', 88);

相关推荐

  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多列对应转行
  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 在Hive中,将多行换为多列可以使用多种方法。其中一种方法是使用SQL语句中的UNION ALL操作符,将多个查询结果合并为一个结果集。例如,可以使用以下SQL语句将多行换为多列: ``` select col1, 'c' as col2, col2 as col3 from col2row1 UNION ALL select col1, 'd' as col2, col3 as col3 from col2row1 UNION ALL select col1, 'e' as col2, col4 as col3 from col2row1; ``` 这个SQL语句将col2row1表中的数据按照指定的列进行组合,每个查询结果都会添加一个新的列,最终得到的结果集将包含多个列。\[2\] 另一种方法是使用Hive中的lateral view和explode函数。这个方法适用于一列中包含多个数据的情况,比如Map或array。可以使用以下SQL语句将多行换为多列: ``` select col1, col2, lv.col3 as col3 from col2row2 lateral view explode(split(col3, ',')) lv as col3; ``` 这个SQL语句使用explode函数将col3列中的数据切分为多个行,并将其展示为多列的形式。\[3\] 总结起来,Hive中可以使用UNION ALL操作符或lateral view和explode函数来实现多行多列的操作。具体使用哪种方法取决于数据的结构和需求。 #### 引用[.reference_title] - *1* *2* *3* [Hive多行多列多列多行](https://blog.csdn.net/weixin_44870066/article/details/128006898)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值