--数据建表
drop table if exists db.tb_name;
create table if not exists db.tb_name
( `name` string
,`class` string
,score string
)
;
insert overwrite table db.tb_name
values
("a","1","100")
,("b","1","99")
,("c ","1","99")
,("d","2","100")
,("e ","2","100")
,("f ","3","86")
,("g","3","99")
,("h","3","99")
;
1.row_number()
select a.*
,row_number() over (partition by `class` order by cast(score as bigint) desc) as rn
from
db.tb_name a
;
查询到的排序结果如下:
2.rank()
select a.*
,rank() over (partition by `class` order by cast(score as bigint) desc) as rn
from
db.tb_name a
;
查询到的排序结果如下:
3.dense_rank()
select a.*
,dense_rank() over (partition by `class` order by cast(score as bigint) desc) as rn
from
db.tb_name a
;
查询到的排序结果如下:
通过以上三种实例演示,发现row_number()不存在相同位次的情况,即使值大小一致,但在排序的时候,也会顺延往下一位。rank()若存在相同位次,下一个位次的计算会跳过之后的位次。dense_rank()若存在相同位次,下一个位次的计算不会跳过之后的位次,即虽然存在相同位次的情况,但是位次是连续不间断的。