Hive增強(qiáng)group函數(shù)

使用場(chǎng)景:需要根據(jù)不同維度對(duì)指標(biāo)進(jìn)行下鉆統(tǒng)計(jì),比如按小時(shí)/按天/按月統(tǒng)計(jì)UV數(shù)。

1、grouping sets

在一個(gè)GROUP BY查詢中,根據(jù)不同的維度組合進(jìn)行聚合,等價(jià)于將不同維度的GROUP BY結(jié)果集進(jìn)行UNION ALL,
其中的GROUPING__ID,表示結(jié)果屬于哪一個(gè)分組集合。

select
    user_type,
    sales,
    count(user_id) as pv,
    GROUPING__ID 
from 
    order_detail
group by 
    user_type,sales
GROUPING SETS(user_type,sales) 
ORDER BY 
    GROUPING__ID;
select
    user_type,
    sales,
    count(user_id) as pv,
    GROUPING__ID 
from 
    order_detail
group by 
    user_type,sales
GROUPING SETS(user_type,sales,(user_type,sales)) 
ORDER BY 
    GROUPING__ID;
2、cube

根據(jù)group by的維度的所有組合進(jìn)行聚合。

select
    user_type,
    sales,
    count(user_id) as pv,
    GROUPING__ID 
from 
    order_detail
group by 
    user_type,sales
WITH CUBE 
ORDER BY 
    GROUPING__ID;
3、rollup

是cube的子集,以最左側(cè)的維度為主,從該維度進(jìn)行層次聚合。

select
    user_type,
    sales,
    count(user_id) as pv,
    GROUPING__ID 
from 
    order_detail
group by 
    user_type,sales
WITH ROLLUP 
ORDER BY 
    GROUPING__ID;
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容