hive grouping sets 等聚合函數

 

函數說明:函數

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

-- 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, rollupcode

相關文章
相關標籤/搜索