Hive中“一行变多行”、“多行合成一行”的实现实例

[ Author: Fu Renjie ]

Ø  一行变多行过程:

// 一行拆分多行的操作:主要是拆分subcat不为空的记录,sql语句如下:

insert into table today_cust_behavior_single

select msisdn ,flow_start,flow_end,kk as subcat

from today_cust_behavior_multi lateral view explode(split(subcat,',')) r1 AS kk

;



Ø  多行变一行过程:

 Insert overwrite table cust_behavior_last_24hour_line

select cc.msisdn, collect_set(cc.s2) from (

select aa.msisdn as msisdn, concat('<',subcat,',',cast(aa.count asSTRING),',', cast(aa.count/bb.s as STRING), '>')  as s2

from (select msisdn, subcat,count 

from cust_behavior_last_24hour order by msisdn, count desc) aa

join   (select msisdn, sum(count) as s

from cust_behavior_last_24hour group bymsisdn) bb

on (aa.msisdn=bb.msisdn)

) cc group by cc.msisdn

;



你可能感兴趣的:(Hive中“一行变多行”、“多行合成一行”的实现实例)