当前位置: 首页 > news >正文

HiveSQL——不使用union all的情况下进行列转行

参考文章:

HiveSql一天一个小技巧:如何不使用union all 进行列转行_不 union all-CSDN博客文章浏览阅读881次,点赞5次,收藏10次。本文给出一种不使用传统UNION ALL方法进行 行转列的方法,其中方法一采用了concat_ws+posexplode()方法,利用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%257D&request_id=170753932316800192292655&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-2-135941286-null-null.nonecase&utm_term=%E7%82%B8%E8%A3%82%E5%93%88%E6%95%B0&spm=1018.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 type

3 小结

   上述案例采用了多种【行转列】的方法,除了常规的union all 上下拼接,还可以利用 concat_ws + posexplode() 结合方式,利用炸裂函数posexplode的下角标pos来实现pos -value的一一对应; 另外还可以利用 explode()+ case when结合方式,用case when进行条件判断,一一匹配。

http://www.lryc.cn/news/300065.html

相关文章:

  • Python环境下基于指数退化模型和LSTM自编码器的轴承剩余寿命预测
  • 无人机竞赛视觉算法开发流程开源计划(询问大家意见)
  • DMA直接内存访问,STM32实现高速数据传输使用配置
  • Web安全研究(六)
  • python3 中try 异常调试 raise 异常抛出
  • Java中的序列化是什么?如何实现对象的序列化和反序列化?请解释Serializable接口的作用是什么?请解释transient关键字的作用是什么?为什么会使用它?
  • 二维差分---三维差分算法笔记
  • D. Divisible Pairs
  • 【教程】Kotlin语言学习笔记(二)——数据类型(持续更新)
  • react 插槽
  • Linux运用fork函数创建进程
  • Pytest测试技巧之Fixture:模块化管理测试数据
  • 设计模式-职责链模式Chain of Responsibility
  • 书生浦语大模型实战营-课程作业(3)
  • 考研英语单词25
  • 计算机网络——08应用层原理
  • 面试计算机网络框架八股文十问十答第五期
  • 拟合案例1:matlab积分函数拟合详细步骤及源码
  • 嵌入式软件设计入门:从零开始学习嵌入式软件设计
  • Educational Codeforces Round 135 (Rated for Div. 2)C. Digital Logarithm(思维)
  • 微信小程序介绍、账号申请、开发者工具目录结构详解及小程序配置
  • 数字的魅力之情有独钟的素数
  • Vue2源码梳理:render函数的实现
  • flask+python企业产品订单管理系统938re
  • Vue2源码梳理:关于数据驱动,与new Vue时的初始化操作
  • 【C++航海王:追寻罗杰的编程之路】关于模板,你知道哪些?
  • 分布式springboot 3项目集成mybatis官方生成器开发记录
  • 算法学习——LeetCode力扣回溯篇4
  • c++ STL系列——(六)multimap
  • Json-序列化字符串时间格式问题