Hive【with as用法】

前言

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

使用HIVE-1180在Hive 0.13.0中添加了通用表格表达式。

语法

withClause: cteClause (, cteClause)*
cteClause: cte_name AS (select statment)

  • 注意

SubQuery Blocks中不支持WITH子句
Views,CTAS和INSERT语句支持CTE。
不支持递归查询。

举例

1、在 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;

2、CTE in Views, CTAS, and Insert Statements

-- insert example
create table s1;
with q1 as ( select key, value from src where key = '5')
from q1
insert overwrite table s1
select *;
 
-- insert 
create table tb_h_test_insert;
WITH TABLE1 AS
(
    SELECT 
    cod_index,
    CAST(test_1 AS VARCHAR(200)), 
    CAST(test_2 AS VARCHAR(200)), 
    CAST(test_3 AS VARCHAR(200))
    FROM db_h_gss.tb_h_test_orig
)
INSERT INTO TABLE tb_h_test_insert PARTITION (cod_index = 1)
SELECT
    test_1,
    test_2,
    test_3
FROM TABLE1 WHERE cod_index = 1;
 
 
-- ctas example
create table s2 as
with q1 as ( select key from src where key = '4')
select * from q1;
 
 
-- view example
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
select * from v1;
  
-- view example, name collision
create view v1 as
with q1 as ( select key from src where key = '5')
select * from q1;
with q1 as ( select key from src where key = '4')
select * from v1;

在第二个View示例中,查询的CTE与创建视图时使用的CTE不同。结果将包含key ='5’的行,因为在视图的查询语句中,视图定义中定义的CTE生效。

另见JIRA:

HIV-1180 支持Hive中的公用表表达式(CTE)

详见官方:

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值