2023.11.15-hivesql之炸裂函数explode练习

把一个容器的多个数据炸裂出单独展示:  explode(容器)

需求:将NBA总冠军球队数据使用explode进行拆分,并且根据夺冠年份进行倒序排序。

1.建表

--step1:建表
create table the_nba_championship(
           team_name string,
           champion_year array<string>
) row format delimited
fields terminated by ','
collection items terminated by '|';

2.加载数据

load data  inpath '/input/The_NBA_Championship.txt' into table the_nba_championship;

 3.验证数据

p3:验证数据
select * from the_nba_championship;

可以使用order by,但效率太低 

--只查询冠军年份,降序排序
select explode(champion_year) as champ
from the_nba_championship order by champ ;  --order by 太占用资源,效率低

使用炸裂函数,配合侧视图,制作新表

-- 将NBA总冠军球队数据使用explode进行拆分,并且根据夺冠年份进行倒序排序。
--先将冠军年各个炸出来
select explode(champion_year) from the_nba_championship;

--再将炸出来的结果用侧视图保存,和原表的球队名字拼起来

with nba as 
    (select team_name, c.champ_year_explode
    from the_nba_championship
    lateral view explode(champion_year) c as champ_year_explode 
    --冠军年份炸开后,存到虚拟表c,并起了个字段名,与原表进行拼接.
    --并将拼接完成的表起了个别名,作为排序查询的准备    
)
select * from nba order by champ_year_explode desc ;

结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白白的wj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值