hive 实现一个字段多行转一行 和 一行转多行

时间:2022-11-29 10:24:41

1.多行转一行

多行转一行可以通过concat_ws(',',collect_set(col_name)) as col_new的方式实现,可以参考:https://www.cnblogs.com/shujuxiong/p/9564556.html

select
       id
       , concat_ws(',',collect_set(col_name)) as col_new
from table_name
group by id
;

 

2.一行转多行

一行转多行通过把原字段中的多个值拆分并转成多条记录的方式实现,lateral view explode(splie(col_name,',')) num as split_col_name

可以参考:https://blog.csdn.net/IBoyMan/article/details/80564166

select
    id
    , col_name
    , split_col_name
from table_name
lateral view explode(splie(col_name,',')) num as split_col_name
group by id
;