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語句結束,不允許單獨使用。