网站建设资讯详细

sql如何按逗号拆分列为多行

发表日期:2023-07-01 15:16:51   作者来源:shuimu   浏览:2275       

思路:

sql如何按逗号拆分列为多行

 

 hive函数 lateral view 主要功能是将原本汇总在一条(行)的数据拆分成多条(行)成虚拟表,再与原表进行笛卡尔积,从而得到明细表。配合UDTF函数使用,一般情况下经常与explode函数搭配,explode的操作对象(列值)是 ARRAY 或者 MAP ,可以通过 split 函数将 String 类型的列值转成 ARRAY 来处理。

【语法格式】

select col_A,col_B,tmp_table.tmp_col 
from test_table 
lateral view explode(split(col_C,'分隔符')) tmp_table as tmp_col
where partition_name='xxx';

【说明】
col_A,col_B,col_C: 都是原表 test_table 的列(字段);
tmp_table:explode形成的新虚拟表,可以不写;
tmp_col:explode 形成的新列(字段);
 

 

create table tmp.user_coupon as 
select *,properties['coupon_id'] coupon_id 
from log.ubs_user_action_hh
where pdate = '2021-09-09'
    and app_client_id = 4
    and module_id = 'coupon_popup'
    and component_id = 'no_c'
    and act_type = 'exposure'
    and properties['coupon_id'] not in ('','')--识别出优惠券id,且优惠券有效期>4天


select * from  tmp.user_coupon limit 100


select user_id,coupon_id,s_name 
from tmp.user_coupon 
lateral view explode(split(coupon_id,','))t as s_name

sql如何按逗号拆分列为多行2