行转列:explode
将输入的一行数组或者map转换成列输出
语法:explode(array (or map))
SELECT explode(myCol) AS myNewCol FROM myTable;
多行转换:lateral view
lateral view用于和split, explode等UDTF一起使用,它能够将一行数据拆成多行数据,在此基础上可以对拆分后的数据进行聚合。
hive> select s.x,sp from test.dual s lateral view explode(split(concat_ws(',','1','2','3','4','5','6','7','8','9'),',')) t as sp;
x sp
a 1
b 2
a 3
from后面是表名,在表名后面加lateral view explode(行转列sql) ,还必须要起一个别名,我这个字段的别名为sp。然后再看看select后面的 s.*,就是原表的字段,我这里面只有一个字段,且为x
应用实例
alter table dw.acoustics.2050_mea_f_t_analysisdata add if not exists partition(date='{DATE}' ,hour='{HOUR}');
insert overwrite table dw.acoustics.2050_mea_f_t_analysisdata partition(date='{DATE}' ,hour='{HOUR}')
select id,
test_id,
sn,
productnumber,
model,
processid,
routeid,
testproject,
frevalue,
frevalue_new,
case when testproject='F0' then '0'
else split(frevalue_new,'_')[0] end as fre_option,
case when testproject='F0' then split(frevalue_new,'_')[0]
else split(frevalue_new,'_')[1] end as fre_value,
ispass,
servertime
from ods.acoustics.sfc_2050_sfc1216j.2050_t_analysisdata
lateral view explode(split(frevalue, ';')) tmpTable as frevalue_new
where date='{DATE}' and hour='{HOUR}' and ServerTime>='2019-11-16'
1349 A1029BFY128873 SFC1216J SLS1216J-01WP F0 0 17122 4 3 612.937658 612.937658 612.937658 0 2019-11-18 09:45:49.403 2020-01-02 07:09:08
173 A1029BCY126745 SFC1216J SLS1216J-01WP THD2-7 600 1997 1 3 200.0_56.113487;600.0_7.856898;1000.0_4.442168 600.0_7.856898 7.856898 0 2019-11-16 09:22:15.99 2020-01-02 07:09:08
来源:CSDN
作者:zhengzaifeidelushang
链接:https://blog.csdn.net/zhengzaifeidelushang/article/details/103870027