天天看點

Hive 行列互換經典題

collect_list( [列名] ) 将某一列轉換成 數組 不去重
collect_set( [列名] ) 将某一列轉換成 數組 去重
concat_ws([分割符],[數組]) 将數組按照分割符轉換成字元串
split( [列名],[分割符] ) 将字元串切分成 數組
explode( [數組] ) 将數組炸開成行
posexplode( [數組] ) 将數組炸開成兩行 (索引 , 值)
t1表 t2表
tag id lable
1,2,3 1 A
1,2 2 B
2,3 3 C
結果表
1,2,3 A,B,C
1,2 A,B
2,3 B,C
select
tmp.tag tag,
concat_ws(',',collect_set(t2.lable)) lab
from 
(
select t1.tag tag,addColumn.tags from t1
lateral view explode(split(t1.tag,',')) addColumn AS tags
) tmp,
t2
where tmp.tags=t2.id
group by tmp.tag
;