select u.id,u.name,c.sp grade_sum
from (select b.user_id,b.sp,rank()over(order by b.sp desc) r
from (select a.user_id,sum(a.p) sp
from (select user_id,grade_num*(if(type = 'add',1,-1)) p
from grade_info)a
group by a.user_id)b)c
left join user u on c.user_id = u.id
where c.r = 1
order by u.id;