-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode1412.sql
More file actions
57 lines (56 loc) · 1.6 KB
/
LeetCode1412.sql
File metadata and controls
57 lines (56 loc) · 1.6 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
-- 找到在exam中,且正序逆序dense_rank都不等于1的student_id
select student_id, student_name
from Student
where
student_id is not null
and student_id not in(
select student_id
from (
select student_id, (
dense_rank() over (
partition by
exam_id
order by score
)
) as "nature_rank", (
dense_rank() over (
partition by
exam_id
order by score desc
)
) as "reverse_rank"
from Exam
) t
where
t.nature_rank = 1
or reverse_rank = 1
)
and student_id in (
select student_id
from Exam
);
-- 利用sum进一步化简判断过程(删除了不需要的其他排名信息,转化为01问题)
select t1.student_id, s.student_name
from (
select *, (
dense_rank() over (
partition by
exam_id
order by score desc
) = 1
) d_rank, (
dense_rank() over (
partition by
exam_id
order by score
) = 1
) a_rank
from Exam
) t1
left join Student s on t1.student_id = s.student_id
group by
t1.student_id
having
sum(d_rank) = 0
and sum(a_rank) = 0
order by student_id