题目链接:点击打开链接
题目大意:注意需要体现出“学校也希望尽可能多的学生能申请该学校”,很多答案解析并没体现出这一条,反而凑巧也过了,估计是测试用例不够全。
解题思路:略。
AC 代码
WITH t AS(SELECT school_id, score, RANK() OVER(PARTITION BY school_id ORDER BY capacity - student_count, score) rk FROM Schools JOIN Exam WHERE capacity - student_count >= 0) SELECT s.school_id, IFNULL(score, -1) score FROM Schools s LEFT JOIN t ON s.school_id = t.school_id AND rk = 1