SQL 코드카타
이번 주 내내 프로젝트 때문에 바쁘다는 핑계로
easy랑 medium만 골라 풀다 보니,
밥에 들어간 콩 골라놓은 것마냥 hard 문제만 드글드글 남았다.
더는 미룰 수 없겠다 싶어서 덤벼든 문제.
문제 링크
팀플 끝나고 풀려니 진짜 머리가 안 돌아간다...
WITH h_senior
AS
(
SELECT employee_id,
salary,
sum(salary) over ( ORDER BY salary) AS "senior_total"
FROM candidates
WHERE experience = 'Senior')
h_junior
AS
(
SELECT employee_id,
salary,
sum(salary) over ( ORDER BY salary) AS "junior_total"
FROM candidates
WHERE experience = 'Junior')
total_seniors
AS
(
SELECT count(employee_id) AS "num_seniors",
max(senior_total) AS "total_senior_salary"
FROM h_senior
WHERE senior_total <= 70000 )
total_juniors
AS
(
SELECT count(employee_id) AS "num_juniors"
FROM h_junior
WHERE senior_total <= 70000 -
(
SELECT ifnull(total_senior_salary,0)
FROM total_seniors )
WITH h_senior
AS (SELECT employee_id,
salary,
Sum(salary)
OVER (
ORDER BY salary) AS "senior_total"
FROM candidates
WHERE experience = 'Senior'),
h_junior
AS (SELECT employee_id,
salary,
Sum(salary)
OVER (
ORDER BY salary) AS "junior_total"
FROM candidates
WHERE experience = 'Junior'),
total_seniors
AS (SELECT Count(employee_id) AS "num_seniors",
Max(senior_total) AS "total_senior_salary"
FROM h_senior
WHERE senior_total <= 70000),
total_juniors
AS (SELECT Count(employee_id) AS "num_juniors"
FROM h_junior
WHERE junior_total <= 70000 - (SELECT Ifnull(total_senior_salary, 0)
FROM total_seniors))
SELECT "Senior" AS "experience",
(SELECT num_seniors
FROM total_seniors) AS "accepted_candidates"
UNION ALL
SELECT "Junior" AS "experience",
(SELECT num_juniors
FROM total_juniors) AS "accepted_candidates";