题解 | #获得积分最多的人(三)#
获得积分最多的人(三)
http://www.nowcoder.com/practice/d2b7e2a305a7499fb310dc82a43820e8
SELECT id,name,grade_sum FROM (-- 获得每个人对应的总分数grade_sum SELECT user_id, grade_sum, RANK() OVER(ORDER BY t.grade_sum DESC) AS rk FROM ( SELECT user_id, SUM(IF(type = 'add',grade_num, -grade_num)) AS grade_sum FROM grade_info GROUP BY user_id ) AS t ) AS m JOIN user AS n ON n.id = m.user_id WHERE rk = 1