hive新功能cube和rollup

一、cube簡稱數據魔方,能夠實現hive多個任意維度的查詢,cube(a,b,c)則首先會對(a,b,c)進行group by,而後依次是(a,b),(a,c),(a),(b,c),(b),(c),最後在對全表進行group by,他會統計所選列中值的全部組合的聚合app

select device_id,os_id,app_id,client_version,from_id,count(user_id) from test_xinyan_reg group by device_id,os_id,app_id,client_version,from_id with cube;it

此語句至關於group by後全部字段的排列組合,而後將結果union all起來io

 rollup能夠實現從右到作遞減多級的統計,顯示統計某一層次結構的聚合。test

至關於:cli


SELECT device_id,null,null,null,null ,count(user_id) FROM test_xinyan_reg group by device_idselect

UNION ALLrollup

SELECT null,os_id,null,null,null ,count(user_id) FROM test_xinyan_reg group by os_idnio

UNION ALL統計

SELECT device_id,os_id,null,null,null ,count(user_id) FROM test_xinyan_reg group by device_id,os_id數據

UNION ALL

SELECT null,null,app_id,null,null ,count(user_id) FROM test_xinyan_reg group by app_id

UNION ALL

SELECT device_id,null,app_id,null,null ,count(user_id) FROM test_xinyan_reg group by device_id,app_id

UNION ALL

SELECT null,os_id,app_id,null,null ,count(user_id) FROM test_xinyan_reg group by os_id,app_id

UNION ALL

SELECT device_id,os_id,app_id,null,null ,count(user_id) FROM test_xinyan_reg group by device_id,os_id,app_id

UNION ALL

SELECT null,null,null,client_version,null ,count(user_id) FROM test_xinyan_reg group by client_version

UNION ALL

SELECT device_id,null,null,client_version,null ,count(user_id) FROM test_xinyan_reg group by device_id,client_version

UNION ALL

SELECT null,os_id,null,client_version,null ,count(user_id) FROM test_xinyan_reg group by os_id,client_version

UNION ALL

SELECT device_id,os_id,null,client_version,null ,count(user_id) FROM test_xinyan_reg group by device_id,os_id,client_version

UNION ALL

SELECT null,null,app_id,client_version,null ,count(user_id) FROM test_xinyan_reg group by app_id,client_version

UNION ALL

SELECT device_id,null,app_id,client_version,null ,count(user_id) FROM test_xinyan_reg group by device_id,app_id,client_version

UNION ALL

SELECT null,os_id,app_id,client_version,null ,count(user_id) FROM test_xinyan_reg group by os_id,app_id,client_version

UNION ALL

SELECT device_id,os_id,app_id,client_version,null ,count(user_id) FROM test_xinyan_reg group by device_id,os_id,app_id,client_version

UNION ALL

SELECT null,null,null,null,from_id ,count(user_id) FROM test_xinyan_reg group by from_id

UNION ALL

SELECT device_id,null,null,null,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,from_id

UNION ALL

SELECT null,os_id,null,null,from_id ,count(user_id) FROM test_xinyan_reg group by os_id,from_id

UNION ALL

SELECT device_id,os_id,null,null,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,os_id,from_id

UNION ALL

SELECT null,null,app_id,null,from_id ,count(user_id) FROM test_xinyan_reg group by app_id,from_id

UNION ALL

SELECT device_id,null,app_id,null,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,app_id,from_id

UNION ALL

SELECT null,os_id,app_id,null,from_id ,count(user_id) FROM test_xinyan_reg group by os_id,app_id,from_id

UNION ALL

SELECT device_id,os_id,app_id,null,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,os_id,app_id,from_id

UNION ALL

SELECT null,null,null,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by client_version,from_id

UNION ALL

SELECT device_id,null,null,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,client_version,from_id

UNION ALL

SELECT null,os_id,null,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by os_id,client_version,from_id

UNION ALL

SELECT device_id,os_id,null,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,os_id,client_version,from_id

UNION ALL

SELECT null,null,app_id,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by app_id,client_version,from_id

UNION ALL

SELECT device_id,null,app_id,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,app_id,client_version,from_id

UNION ALL

SELECT null,os_id,app_id,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by os_id,app_id,client_version,from_id

UNION ALL

SELECT device_id,os_id,app_id,client_version,from_id ,count(user_id) FROM test_xinyan_reg group by device_id,os_id,app_id,client_version,from_id

UNION ALL

SELECT null,null,null,null,null ,count(user_id) FROM test_xinyan_reg

 

二、 rollup能夠實現從右到左遞減多級的統計,顯示統計某一層次結構的聚合。

 select device_id,os_id,app_id,client_version,from_id,count(user_id) from test_xinyan_reg group by device_id,os_id,app_id,client_version,from_id with rollup;

至關於

 select device_id,os_id,app_id,client_version,from_id,count(user_id) 

from test_xinyan_reg 

group by device_id,os_id,app_id,client_version,from_id 

grouping sets ((device_id,os_id,app_id,client_version,from_id),(device_id,os_id,app_id,client_version),(device_id,os_id,app_id),(device_id,os_id),(device_id),());

相關文章
相關標籤/搜索