hive中行转列,列转行的各种情况及解决方法

1、多行行转列和列转行

行转列:一般用case when,再做一个group by 去掉0值。

列转行:union all

例如:

nameyearsa
xiaohong20001000
dahei20012000
dahei20001000
dahei20012000

转为

nameyear1year2
xiaohong10002000
dahei10002000
select name,
sum(case year when '2001' then sa else 0 end) year1,
sum(case year when '2002' then sa else 0 end) year2,
from a group by name;

反过来则为

select name,'2000' as year,year1 as sa from a
union all
select name,'2001' as year,year2 as sa from a

2、多行转一列,concat_ws,结合collect_set|collect_list

将第一张表转为下表

namedetail
dahei2000:1000,2001:2000
select name,
concat_ws(',',collect_set(concat(year,':',cast(sa as string)))) as detail
from a group by name;

3、一列转多列,如把上表分开,可用split,substring等字符串处理函数。

4、一行转多行,lateral view 子窗口结合 explode

如将上表转换成

namedetail
dahei2000:1000
dahei2001:2000
select name,detail from a lateral view explode(split(detail,'\\,')) a as detail;

如果应用多行 lateral view explode则做笛卡尔积。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值