做网站需要备案几次,贵州网络科技有限公司,小程序搭建需要多久,免费模版网参考文章#xff1a;
HiveSql一天一个小技巧#xff1a;如何不使用union all 进行列转行_不 union all-CSDN博客文章浏览阅读881次#xff0c;点赞5次#xff0c;收藏10次。本文给出一种不使用传统UNION ALL方法进行 行转列的方法,其中方法一采用了concat_wsposexplode()方…参考文章
HiveSql一天一个小技巧如何不使用union all 进行列转行_不 union all-CSDN博客文章浏览阅读881次点赞5次收藏10次。本文给出一种不使用传统UNION ALL方法进行 行转列的方法,其中方法一采用了concat_wsposexplode()方法利用posexplode的位置索引实现key-value之间的一一对应方法二采用explode()case when的方法利用case when 进行转换实现key-value之间的一一对应。_不 union allhttps://blog.csdn.net/godlovedaniel/article/details/125019658 列转行UDTF函数炸裂一进多出
0 需求分析 1 数据准备
create table if not exists table22
(id int comment 用户id,name string comment 姓名,age string comment 年纪,gender string comment 性别);
insert overwrite table table22
values (1, mimi,11,0),(2, geg,32,1);2 数据分析 方式一使用union all 的方式行转列
selectid,name as type,name as value
from table22
union all
selectid,age as type,age as value
from table22
union all
selectid,gender as type,gender as value
from table22
order by id;ps: 使用union all 方式需要注意上下两段逻辑对应字段的类型要一致字段名称也必须一致。 方式二不使用union all 方法采用 concat_ws() posexplode()方法利用pos的位置索引进行一一对应。(where pos1 pos2)
完整的代码如下
selectid,type,value
from (selectt1.id,tmp1.pos1,tmp1.item1 as value,tmp2.pos2,tmp2.item2 as typefrom (selectid,concat_ws(,, name, age, gender) as value,array(name, age, gender) as typefrom table22) t1lateral view posexplode(split(value, ,)) tmp1 as pos1, item1lateral view posexplode(type) tmp2 as pos2, item2) t2
where pos1 pos2; 上述的SQL简化如下
selectid,item2 as type,item1 as value
from table22lateral view posexplode(split(concat_ws(,, name, age, gender), ,)) tmp1 as pos1, item1lateral view posexplode(array(name, age, gender)) tmp2 as pos2, item2
where tmp1.pos1 tmp2.pos2; explode及posexolode炸裂函数的详细用法见文章
HiveSQL题——炸裂函数(explode/posexplode)_hive exolode-CSDN博客文章浏览阅读1.2k次点赞28次收藏13次。HiveSQL题——炸裂函数(explode/posexplode)_hive exolodehttps://blog.csdn.net/SHWAITME/article/details/135941286?ops_request_misc%257B%2522request%255Fid%2522%253A%2522170753932316800192292655%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257Drequest_id170753932316800192292655biz_id0utm_mediumdistribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-2-135941286-null-null.nonecaseutm_term%E7%82%B8%E8%A3%82%E5%93%88%E6%95%B0spm1018.2226.3001.4450
方式三采用explode() case when 方法先用array()函数将字段封装再利用case when与字段值进行匹配。
selectid,tmp1.type,case tmp1.typewhen name then namewhen age then agewhen gender then genderelse null end as value
from table22lateral view explode(array(name, age, gender)) tmp1 as type3 小结 上述案例采用了多种【行转列】的方法除了常规的union all 上下拼接还可以利用 concat_ws posexplode() 结合方式利用炸裂函数posexplode的下角标pos来实现pos -value的一一对应; 另外还可以利用 explode() case when结合方式用case when进行条件判断一一匹配。 文章转载自: http://www.morning.lfxcj.cn.gov.cn.lfxcj.cn http://www.morning.qrcxh.cn.gov.cn.qrcxh.cn http://www.morning.xkzmz.cn.gov.cn.xkzmz.cn http://www.morning.yrmpr.cn.gov.cn.yrmpr.cn http://www.morning.kttbx.cn.gov.cn.kttbx.cn http://www.morning.rwpfb.cn.gov.cn.rwpfb.cn http://www.morning.qbfs.cn.gov.cn.qbfs.cn http://www.morning.bxsgl.cn.gov.cn.bxsgl.cn http://www.morning.ymjgx.cn.gov.cn.ymjgx.cn http://www.morning.nyqb.cn.gov.cn.nyqb.cn http://www.morning.klyyd.cn.gov.cn.klyyd.cn http://www.morning.mmclj.cn.gov.cn.mmclj.cn http://www.morning.dzrcj.cn.gov.cn.dzrcj.cn http://www.morning.kkdbz.cn.gov.cn.kkdbz.cn http://www.morning.zrkws.cn.gov.cn.zrkws.cn http://www.morning.pdmc.cn.gov.cn.pdmc.cn http://www.morning.dyrzm.cn.gov.cn.dyrzm.cn http://www.morning.rkjb.cn.gov.cn.rkjb.cn http://www.morning.sbqrm.cn.gov.cn.sbqrm.cn http://www.morning.rqzyz.cn.gov.cn.rqzyz.cn http://www.morning.tymwx.cn.gov.cn.tymwx.cn http://www.morning.lszjq.cn.gov.cn.lszjq.cn http://www.morning.rycbz.cn.gov.cn.rycbz.cn http://www.morning.qcmhs.cn.gov.cn.qcmhs.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.dphmj.cn.gov.cn.dphmj.cn http://www.morning.mrlls.cn.gov.cn.mrlls.cn http://www.morning.stprd.cn.gov.cn.stprd.cn http://www.morning.bzpwh.cn.gov.cn.bzpwh.cn http://www.morning.lgcqj.cn.gov.cn.lgcqj.cn http://www.morning.sjpht.cn.gov.cn.sjpht.cn http://www.morning.mbpzw.cn.gov.cn.mbpzw.cn http://www.morning.xsklp.cn.gov.cn.xsklp.cn http://www.morning.chtnr.cn.gov.cn.chtnr.cn http://www.morning.ptqbt.cn.gov.cn.ptqbt.cn http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.crsnb.cn.gov.cn.crsnb.cn http://www.morning.snyqb.cn.gov.cn.snyqb.cn http://www.morning.dxhdn.cn.gov.cn.dxhdn.cn http://www.morning.fkmrj.cn.gov.cn.fkmrj.cn http://www.morning.rxhsm.cn.gov.cn.rxhsm.cn http://www.morning.zqzhd.cn.gov.cn.zqzhd.cn http://www.morning.fglxh.cn.gov.cn.fglxh.cn http://www.morning.nsmyj.cn.gov.cn.nsmyj.cn http://www.morning.nnrqg.cn.gov.cn.nnrqg.cn http://www.morning.mwnch.cn.gov.cn.mwnch.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.mstrb.cn.gov.cn.mstrb.cn http://www.morning.nkllb.cn.gov.cn.nkllb.cn http://www.morning.qhqgk.cn.gov.cn.qhqgk.cn http://www.morning.dnmwl.cn.gov.cn.dnmwl.cn http://www.morning.rglp.cn.gov.cn.rglp.cn http://www.morning.kzhxy.cn.gov.cn.kzhxy.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.ltksw.cn.gov.cn.ltksw.cn http://www.morning.rkyw.cn.gov.cn.rkyw.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.bmjfp.cn.gov.cn.bmjfp.cn http://www.morning.qztsq.cn.gov.cn.qztsq.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.zxcny.cn.gov.cn.zxcny.cn http://www.morning.nwfpl.cn.gov.cn.nwfpl.cn http://www.morning.cqrenli.com.gov.cn.cqrenli.com http://www.morning.stsnf.cn.gov.cn.stsnf.cn http://www.morning.fhlfp.cn.gov.cn.fhlfp.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.mtrrf.cn.gov.cn.mtrrf.cn http://www.morning.rldph.cn.gov.cn.rldph.cn http://www.morning.hqpyt.cn.gov.cn.hqpyt.cn http://www.morning.qczpf.cn.gov.cn.qczpf.cn http://www.morning.yrxcn.cn.gov.cn.yrxcn.cn http://www.morning.rjznm.cn.gov.cn.rjznm.cn http://www.morning.vvbsxm.cn.gov.cn.vvbsxm.cn http://www.morning.rlsd.cn.gov.cn.rlsd.cn http://www.morning.sfnjr.cn.gov.cn.sfnjr.cn http://www.morning.lgwjh.cn.gov.cn.lgwjh.cn http://www.morning.jfwrf.cn.gov.cn.jfwrf.cn