-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1112-HighestGradeForEachStudent.sql
More file actions
85 lines (79 loc) · 2.4 KB
/
1112-HighestGradeForEachStudent.sql
File metadata and controls
85 lines (79 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
-- 1112. Highest Grade For Each Student
-- Table: Enrollments
-- +---------------+---------+
-- | Column Name | Type |
-- +---------------+---------+
-- | student_id | int |
-- | course_id | int |
-- | grade | int |
-- +---------------+---------+
-- (student_id, course_id) is the primary key (combination of columns with unique values) of this table.
-- grade is never NULL.
-- Write a solution to find the highest grade with its corresponding course for each student.
-- In case of a tie, you should find the course with the smallest course_id.
-- Return the result table ordered by student_id in ascending order.
-- The result format is in the following example.
-- Example 1:
-- Input:
-- Enrollments table:
-- +------------+-------------------+
-- | student_id | course_id | grade |
-- +------------+-----------+-------+
-- | 2 | 2 | 95 |
-- | 2 | 3 | 95 |
-- | 1 | 1 | 90 |
-- | 1 | 2 | 99 |
-- | 3 | 1 | 80 |
-- | 3 | 2 | 75 |
-- | 3 | 3 | 82 |
-- +------------+-----------+-------+
-- Output:
-- +------------+-------------------+
-- | student_id | course_id | grade |
-- +------------+-----------+-------+
-- | 1 | 2 | 99 |
-- | 2 | 2 | 95 |
-- | 3 | 3 | 82 |
-- +------------+-----------+-------+
SELECT
a.student_id AS student_id,
MIN(a.course_id) AS course_id, -- 若科目成绩并列,取 course_id 最小的一门
b.grade AS grade
FROM
Enrollments AS a,
(
SELECT
student_id,
MAX(grade) AS grade
FROM
Enrollments
GROUP BY
student_id
) AS b
WHERE
a.student_id = b.student_id AND
a.grade = b.grade
GROUP BY
a.student_id
ORDER BY
a.student_id ASC -- 查询结果需按 student_id 增序进行排序
-- best solution
SELECT
student_id, course_id, grade
FROM
(
SELECT
*,
-- partition by student_id 按 student_id 分区
-- order by grade desc, course_id 按 成绩(desc) 课程ID(asc) 排序
rank() OVER (
PARTITION BY student_id
ORDER BY grade DESC, course_id ASC
) 'rk'
FROM
Enrollments
) tb
WHERE
rk = 1
ORDER BY
student_id ASC