根据得分求排名情况,显然存在得分并列的情况。

create table if not exists busi.user_rank
(
    uname varchar(200),
    score int
);

comment on column busi.user_rank.score is '得分';
insert into busi.user_rank (uname, score) values ('小明',88);
insert into busi.user_rank (uname, score) values ('小王',88);
insert into busi.user_rank (uname, score) values ('小刘',98);
insert into busi.user_rank (uname, score) values ('小张',70);
insert into busi.user_rank (uname, score) values ('小张',null);

postgresql:

select uname,
       score,
       rank() over (order by score desc nulls last )
from busi.user_rank

查询结果:

小刘	98	1
小明	88	2
小王	88	2
小张	70	4
小张		5

不考虑并列的情况

select uname,
       score,
       row_number() over (order by score desc nulls last )
from busi.user_rank;
小刘	98	1
小明	88	2
小王	88	3
小张	70	4
小张		5