说明:code
row_number() over ([partition col1] [order by col2])排序
rank() over ([partition col1] [order by col2])it
dense_rank() over ([partition col1] [order by col2])io
它们都是根据col1字段分组,而后对col2字段进行排序,对排序后的每行生成一个行号,这个行号从1开始递增test
col一、col2均可以是多个字段,用‘,‘分隔select
区别:分页
1)row_number:无论col2字段的值是否相等,行号一直递增,好比:有两条记录的值相等,但一个是第一,一个是第二统计
2)rank:上下两条记录的col2相等时,记录的行号是同样的,但下一个col2值的行号递增N(N是重复的次数),好比:有两条并列第一,下一个是第三,没有第二查询
3)dense_rank:上下两条记录的col2相等时,下一个col2值的行号递增1,好比:有两条并列第一,下一个是第二co
业务实例:
统计每一个学科的前三名
select * from (select *, row_number() over (partition by sub order by score desc) as od from t ) t where od<=3;
语文成绩是80分的排名是多少
hive (test)> select od from (select *, row_number() over (partition by sub order by score desc) as od from t ) t where sub=‘chinese‘ and score=80;
分页查询
hive (test)> select * from (select *, row_number() over () as rn from t) t1 where rn between1 and 5;