-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetCode569.sql
More file actions
30 lines (29 loc) · 898 Bytes
/
LeetCode569.sql
File metadata and controls
30 lines (29 loc) · 898 Bytes
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
with
cte as (
select e.id, e.company, e.salary, ROW_NUMBER() over (
partition by
e.company
order by e.salary
) "rankNum", c.counts, c.odd_even
from Employee e
left join (
select company, count(*) counts, (
CASE
when count(*) % 2 = 0 then "even"
else "odd"
END
) odd_even
from Employee
group by
company
) c on e.company = c.company
)
select id, company, salary
from cte
-- 这里也可以使用逻辑运算符实现相同效果
where (
CASE
when odd_even = "odd" then rankNum = (counts + 1) / 2
else rankNum in (counts / 2, counts / 2 + 1)
END
);