【hive】with as语法

公用表表达式(CTE)在Hive中通过WITH子句提供,它创建一个临时结果集,可在同一个SQL语句中多次使用。CTE在执行范围仅限于单个语句,适用于SELECT,INSERT,CREATE TABLE AS SELECT或CREATE VIEW AS SELECT操作。本文讨论如何在Hive的查询和视图创建中应用CTE以优化SQL。
摘要由CSDN通过智能技术生成

公用表表达式(CTE)是从WITH子句中指定的简单查询派生的临时结果集(会把查询的表数据放到内存中,供其他查询随时使用),该子句紧跟在SELECT或INSERT关键字之前。CTE仅在单个语句的执行范围内定义。可以在Hive SELECT,INSERT, CREATE TABLE AS SELECT或CREATE VIEW AS SELECT语句中使用一个或多个CTE 。

在 select 中使用 CTE

with q1 as ( select key from src where key = '5')
select *
from q1;
 
-- from style
with q1 as (select * from src where key= '5')
from q1
select *;
  
-- chaining CTEs
with q1 as ( select key from q2 where key = '5'),
q2 as ( select key from src where key = '5')
select * from (select key from q1) a;
  
-- union example
with q1 as (select * from src where key= '5'),
q2 as (select * from src s2 where key = '4')
select * from q1 union all select * from q2;

CTE in Views, CTAS, and Insert Statements

-- insert example
create table s1 like src;
with q1 as ( select key, value from src where key = '5&
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值