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 数据准备

sql 复制代码
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 的方式行转列

sql 复制代码
select
    id,
    'name' as type,
    name   as value
from table22
union all
select
    id,
    'age' as type,
    age   as value
from table22
union all
select
    id,
    'gender' as type,
    gender   as value
from table22
order by id;

ps: 使用union all 方式需要注意:上下两段逻辑,对应字段的类型要一致,字段名称也必须一致

方式二: 不使用union all 方法,采用 concat_ws() + posexplode( )方法,利用pos的位置索引进行一一对应。(where pos1 = pos2)

完整的代码如下:

sql 复制代码
select
    id,
    type,
    value
from (
         select
             t1.id,
             tmp1.pos1,
             tmp1.item1 as value,
             tmp2.pos2,
             tmp2.item2 as type
         from (
                  select
                      id,
                      concat_ws(',', name, age, gender) as value,
                      array('name', 'age', 'gender')    as type
                  from table22
              ) t1
                  lateral view posexplode(split(value, ',')) tmp1 as pos1, item1
                  lateral view posexplode(type) tmp2 as pos2, item2
     ) t2
where pos1 = pos2;

上述的SQL简化如下:

sql 复制代码
select
    id,
    item2 as type,
    item1 as value
from table22
         lateral view posexplode(split(concat_ws(',', name, age, gender), ',')) tmp1 as pos1, item1
         lateral 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与字段值进行匹配。

sql 复制代码
select
    id,
    tmp1.type,
    case tmp1.type
        when 'name' then name
        when 'age' then age
        when 'gender' then gender
        else null end as value
from table22
   lateral view explode(array('name', 'age', 'gender')) tmp1 as type

3 小结

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

相关推荐
杂家2 小时前
Hadoop完全分布式部署(超详细)
大数据·hadoop·分布式
BD_Marathon2 小时前
【Hadoop】hadoop3.3.1完全分布式配置
大数据·hadoop·分布式
Ashlee_code3 小时前
BSS供应商:电信与金融领域的幕后支撑者
大数据·网络·金融·系统架构·跨境·金融机构·场外期权
Acrelhuang6 小时前
覆盖全场景需求:Acrel-1000 变电站综合自动化系统的技术亮点与应用
大数据·网络·人工智能·笔记·物联网
忙碌5446 小时前
智能应用开发指南:深度学习、大数据与微服务的融合之道
大数据·深度学习·微服务
萤丰信息6 小时前
智慧园区系统:开启园区管理与运营的新时代
java·大数据·人工智能·安全·智慧城市·智慧园区
Q26433650237 小时前
【有源码】基于Hadoop+Spark的起点小说网大数据可视化分析系统-基于Python大数据生态的网络文学数据挖掘与可视化系统
大数据·hadoop·python·信息可视化·数据分析·spark·毕业设计
洞见新研社7 小时前
家庭机器人,从科幻到日常的二十年突围战
大数据·人工智能·机器人
筑梦之人8 小时前
Spark-3.5.7文档2 - RDD 编程指南
大数据·分布式·spark
艾莉丝努力练剑10 小时前
【C++:红黑树】深入理解红黑树的平衡之道:从原理、变色、旋转到完整实现代码
大数据·开发语言·c++·人工智能·红黑树