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

  • Post author:
  • Post category:其他


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(split(

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(split(col_name,',')) num as split_col_name
-- group by 是为了去重,可以不要
group by id, col_name, split_col_name ;

转载于:https://www.cnblogs.com/shujuxiong/p/9925385.html