题解 | #获得积分最多的人(三)#
获得积分最多的人(三)
https://www.nowcoder.com/practice/d2b7e2a305a7499fb310dc82a43820e8
select b.user_id user_id
,u.name name
,b.grade_num grade_num
from
(
select user_id
,sum(if(type = 'add',grade_num,grade_num*(-1))) grade_num
,dense_rank() over(order by sum(if(type = 'add',grade_num,grade_num*(-1))) desc) dr
from grade_info
group by 1 #求出用户当前总积分和其排名
)b
left join user u on b.user_id = u.id
where b.dr = 1
order by 1