hive中使用with as查询作为临时中间表

oracle、hive都存在with as的用法。用于将重复的查询结果复用。

今天做统计分析时用到,使用例子如下:

1. 直接查询

with tmp_a as (
select f1,f2,f3 from test1
)
select f1,f2,f3 from tmp_a;

2. 多表计算结果join

with tmp_a as (
select f1,f2,f3 from test1
),
tmp_b as(
select f1,f4,f5 from test2
)
select a.f1,a.f2,a.f3,b.f4,b.f5 from
tmp_a a
left join
tmp_b b
on a.f1 = b.f1

 注意点:

with as 最后必须跟sql语句结束,不允许单独使用。

原文地址:https://www.cnblogs.com/30go/p/10116815.html