使用場(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;
