Hive学习笔记:with as子查询

一、说明

与其他 SQL 语法类似,Hive 中也支持 with as 将一大段 SQL 语句封装为子查询,方便后续多次调用。

MySQL旧版本不支持with as语法,8.0才支持。

with tt as
(
	select *,
            row_number() over(partition by id order by score desc) rn 
    from table_name
)
select * from tt where rn = 1;

Hive 可以通过 with 查询来提高查询性能,因为先通过 with 语句将数据查询到内存中,后续查询可以直接调用。

类似于视图、临时表,不过是一次性的,但是可以大大简化后续SQL。

二、注意

1.with 子句必须在引用的 select 语句之前定义,而且后面必须要跟 select 查询,否则报错。

2.with as 后面不能加分号,with 关键字在同级中只能使用一次,允许跟多个子句,用逗号隔开,最后一个子句与后面的查询语句之间只能用右括号分隔,不能用逗号。

create table table_new as
with t1 as
(
	select * from table_first
),
t2 as
(
	select * from table_seconde
),
t3 as
(
	select * from table_third
)
select * from t1, t2, t3;

3.前面的 with 子句定义的查询在后面的 with 子句中可以使用。但是一个 with 子句内部不能嵌套 with 子句。

with t1 as
(
	select * from table_first
),
t2 as
(
	select * from t1
)
select * from t2;

参考链接:Hive(二):with as用法

参考链接:Hive中使用 with as 优化SQL

posted @   Hider1214  阅读(1444)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2019-11-19 蒙特卡罗算法:模拟
点击右上角即可分享
微信分享提示