一、行转列express
场景:在hive表中,一个用户会有多我的群标签,List格式(逗号分隔如要转成List),有时咱们须要统计一我的群标签下有少用户,这是就须要使用行转列了segmentfault
例如,user_crowd_info有以下数据ide
visit_id crowds abc [100,101,102] def [100,101] abe [101,105]
能够使用的函数函数
select explode(crowds) as crowd from user_crowd_info; 结果: 100 101 102 100 101 101 105
这样执行的结果只有crowd, 可是咱们须要完整的信息,使用select visit_id, explode(crowds) as crowd from user_crowd_info;
是不对的,会报错UDTF's are not supported outside the SELECT clause, nor nested in expressions
学习
因此咱们须要这样作:区块链
select visit_id,crowd from user_crowd_info t lateral view explode(t.crowds) adtable as crowd;
二、列转行spa
使用concat_ws函数便可code
select visit_id,concat_ws(,crowd) from user_crowd_info group by visit_id;