1、hive的分組和組內排序---語法
語法:
row_number() over (partition by 字段a order by 計算項b desc ) rank
- rank是排序的別名
- partition by:類似hive的建表,分區的意思;
- order by :排序,默認是升序,加desc降序;
- 這里按字段a分區,對計算項b進行降序排序
2、hive的分組和組內排序 --- 實例
要取top10品牌,各品牌的top10渠道,各品牌的top10渠道中各渠道的top10檔期
1、取top10品牌
select “品牌” , sum/count/其他() as num from "table_name" order by num desc limit 10;
2、取top10品牌下各品牌的top10渠道
select a.* from (select "品牌","渠道",sum/count() as num, row_number () over (partition by "品牌" order by num desc) rank from “table_name” where 品牌限制條件 group by “品牌”,“渠道” ) a having a.rank <= 10;
3、 取top10品牌下各品牌的top10渠道中各渠道的top10檔期
select a.* from (select "品牌","渠道","檔期",sum/count/其他() as num row_number() over (partition by "檔期" order by num desc) rank from "table_name" where 品牌限制條件 group by “品牌”,“渠道) a Having a.rank <= 10;
row_number的使用在hive和spark的實時計算中常常會用到計算分區中的排序問題,所以使用好row_number是很重要的。
作者:跨界師
鏈接:https://www.jianshu.com/p/51599bab0c00
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。