-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path2010.sql
34 lines (31 loc) · 860 Bytes
/
2010.sql
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
-- [ LeetCode ] 2010. The Number of Seniors and Juniors to Join the Company II
WITH CumulativeSalaries (employee_id, experience, cumulative_salary) AS (
SELECT
employee_id,
experience,
SUM(salary) OVER(PARTITION BY experience ORDER BY salary ASC) AS cumulative_salary
FROM Candidates
), Seniors (employee_id, cumulative_salary) AS (
SELECT
employee_id,
cumulative_salary
FROM CumulativeSalaries
WHERE (
experience = 'Senior'
AND
cumulative_salary <= 70000
)
), Juniors (employee_id) AS (
SELECT employee_id
FROM CumulativeSalaries
WHERE (
experience = 'Junior'
AND
cumulative_salary <= 70000 - (SELECT IFNULL(MAX(cumulative_salary), 0) FROM Seniors)
)
)
SELECT employee_id
FROM Seniors
UNION ALL
SELECT employee_id
FROM Juniors;