hive grouping sets 等聚合函數


 

函數說明:

grouping sets
在一個 group by 查詢中,根據不同的維度組合進行聚合,等價於將不同維度的 group by 結果集進行 union all
cube
根據 group by 的維度的所有組合進行聚合
rollup
是 cube 的子集,以最左側的維度為主,從該維度進行層級聚合。

-- grouping sets 
select 
       order_id,
       departure_date,
       count(*) as cnt
  from ord_test
 where order_id=410341346
 group by order_id,
       departure_date
 grouping sets (order_id,(order_id,departure_date))
;

---- 等價於以下
group by order_id
union all
group by order_id,departure_date

-- cube
select 
       order_id,
       departure_date,
       count(*) as cnt
  from ord_test
 where order_id=410341346
 group by order_id,
       departure_date
 with cube
 ;

---- 等價於以下
select count(*) as cnt from ord_test where order_id=410341346
union all
group by order_id
union all
group by departure_date
union all
group by order_id,departure_date

-- rollup
select 
       order_id,
       departure_date,
       count(*) as cnt
  from ord_test
 where order_id=410341346
 group by order_id,
       departure_date
 with rollup
 ;

---- 等價於以下
select count(*) as cnt from ord_test where order_id=410341346
union all
group by order_id
union all
group by order_id,departure_date

結果:grouping_sets, cube, rollup

 


免責聲明!

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



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