原始表(mytest)
id |
uid |
date |
score |
1 |
2 |
2023-06-07 |
50 |
2 |
3 |
2023-06-07 |
10 |
3 |
2 |
2023-06-21 |
55 |
4 |
2 |
2023-06-21 |
50 |
5 |
3 |
2023-06-06 |
45 |
6 |
2 |
2023-06-23 |
26 |
实现排名统计
select
zb.*,
cb.topn as od1,
cb1.topn as od2,
cb2.topn as od3
from
mytest zb
left join(
select score,(select COUNT(*)+ 1 from (select distinct score from mytest) b
where b.score>a.score ) as topn
from(
select distinct score from mytest
) a
) cb on zb.score = cb.score
left join (
select uid,score,(select COUNT(*)+ 1 from (select distinct uid,score from mytest) b
where a.uid = b.uid and b.score>a.score) as topn
from
(
select distinct uid,score from mytest
) a
) cb1 on cb1.uid = zb.uid and zb.score = cb1.score
left join (
select a.id,(select count(*)+ 1 from mytest b where a.score<b.score and a.id <> b.id) topn
from mytest a
) cb2 on cb2.id = zb.id
order by zb.score desc