SQL 코드카타
이 문제에서는 단순히 percent_rank를 쓰면 답을 얻을 수 없다.
팀의 전체 갯수에 따라 0.33과 0.66의 경계가 모호할 수 있기 때문.
이것 때문에 tier를 맞추면 position이 틀어지고
position을 맞춰놓으면 tier가 틀어지는 상황이 생겼다.
결국 position과 rank부터 먼저 계산해 놓고,
position과 팀 수 전체(count)를 기반으로
1/3, 2/3를 따로따로 구해서 tier를 정했다.
이렇게 하면 정답.
WITH rankedteams
AS (SELECT team_name,
3 * wins + 1 * draws AS points,
Rank()
OVER(
ORDER BY 3*wins + 1*draws DESC) AS position
FROM teamstats),
tieredteams
AS (SELECT team_name,
points,
position,
CASE
WHEN position <= Ceil((SELECT Count(*)
FROM rankedteams) * 1.0 / 3) THEN
'Tier 1'
WHEN position <= Ceil((SELECT Count(*)
FROM rankedteams) * 2.0 / 3) THEN
'Tier 2'
ELSE 'Tier 3'
END AS tier
FROM rankedteams)
SELECT team_name,
points,
position,
tier
FROM tieredteams
ORDER BY 2 DESC,
1 ASC;
SELECT e1.employee_id,
Count(*) AS "overlapping_shifts"
FROM employeeshifts e1
JOIN employeeshifts e2
ON e1.employee_id = e2.employee_id
WHERE e1.start_time < e2.start_time
AND e1.end_time > e2.start_time
GROUP BY 1
ORDER BY 1;