Hive - 行转列,列转行

HOUHUILIN / 2024-03-06 / 原文

 

行转列(多行转一行)

1、创建表,并插入示例数据。

create table  students_info
(`SNO` string comment '学生编号',
`name` string comment '姓名',
`DEPART` string  comment '选修课程'
)
--学生信息表数据插入
insert into  students_info  values 
(103,'张三','心理学'),
(103,'张三','Java'),
(105,'王五','Spark'),
(109,'李麻子','Flink'),
(109,'李麻子','Kylin');

select * from  students_info

2、行转列

select 
    max(sno), 
    name,
    concat_ws(',', collect_set(DEPART)) as DEPART 
from students_info
group by name

 

 

列转行