Hive中row_number的使用


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
來源:簡書
著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM