hive-行转列列转行

一、

 

idstype
1A
1C
1E
2B
2D
2F

 

idsABCDEF
1101010
2010101

将第一个表格的数据变成第二种

两种方式:1. 

 select ids, 
 count(if(type='A',1,null))A,
 count(if(type='B',1,null))B,
 count(if(type='C',1,null))C,
 count(if(type='D',1,null))D,
 count(if(type='E',1,null))E,
 count(if(type='F',1,null))F from lp.study group by ids;

2.

 select ids,sum(if(type='A',1,0))A,
 sum(if(type='B',1,0))B,
 sum(if(type='C',1,0))C,
 sum(if(type='D',1,0))D,
 sum(if(type='E',1,0))E,
 sum(if(type='F',1,0))F from lp.study group by ids;

 

二.行转列 使用 collect_set

 

idstype
1A
1C
1E
2B
2D
2F

 

idstype
1A,C,E
2B,D,F

将第一个表格转为第二个表格

 

select ids,concat_ws(",",collect_set(type)) types  from lp.study group by ids;

执行下个句子了解

select ids,concat_ws(',',collect_set(type)) types
from
(
select '1' ids,'A' type
union all
select '1' ids,'B' type
union all
select '1' ids,'C' type
union all
select '2' ids,'E' type
union all
select '2' ids,'F' type
union all
select '2' ids,'D' type
) a
group by ids;

三.列传行 使用 lateral view explode

idstype
1A,C,E
2B,D,F

 

idstype
1A
1C
1E
2B
2D
2F

将第一个表格转为第二个表格

select ids,type from 
 (select ids,concat_ws(",",collect_set(type)) types  from lp.study group by ids) a lateral view explode(split(types,",")) col as type ;

执行下面这个句子了解

select ids,type from
(
select '1' ids,'A,B,C' types
union all
select '2' ids,'D,E,F' types
)
a lateral view explode(split(types,",")) col as type ;

Hive中的列转行行转列是通过使用一些特定的内置函数和关键字来实现的。列转行可以使用EXPLODE函数,该函数可以将一个包含复杂结构的数组或者映射拆分成多行。行转列可以使用collect_set函数,该函数将某一列的所有数据转化为一个集合,并且可以使用concat_ws函数将集合中的所有元素以逗号分割连接成一个字符串。此外,为了使用EXPLODE和LATERAL VIEW函数,你可以使用LATERAL VIEW关键字,语法为LATERAL VIEW udtf(expression) tableAlias AS columnAlias。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [hive列转行案例](https://download.csdn.net/download/weixin_38581777/14037437)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [hive行转列列转行](https://blog.csdn.net/qq_24790473/article/details/109710145)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [hive操作(行转列列转行)](https://blog.csdn.net/aiduo3346/article/details/102085019)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值