hive with as 語法


簡介

with...as...需要定義一個sql片段,會將這個片段產生的結果集保存在內存中,
后續的sql均可以訪問這個結果集和,作用與視圖或臨時表類似.

語法說明

  1. with...as...必須和其他語句一起使用
  2. with...as...是一次性的

with...as...的示例如下:

-- with table_name as(子查詢語句) 其他sql 
with tmp as (
    select * from xxx
)
select * from tmp;

 

同級的多個臨時表之間用,as后的子句必須用(),


with tmp1 as (
    select * from xxx
),tmp2 as (
    select * from xxx
)
select * from tmp1,tmp2;

with...as...使用嵌套的例子:

with tmp2 as (
    with tmp1 as (
        select * from xxx
    )
    select * from tmp1
)
select * from tmp2;

 

優點

  1. 提高代碼可讀性(結構清晰)
  2. 簡化sql,優化執行速度(with子句只需要執行一次)



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM