문제 이해
↑Challenges table
문제 이해 포인트
생각 정리
1.Submission 테이블에서 해커 이름 붙이기
2. submission 테이블의 challenge_id에 맞는 score 가져오기
ㄴ difficulty_table 사용
3. 1,2에서 합친 테이블에서 s.score와 c.score가 동일한 rows 가져오기 / challenge 수 카운팅 하기
4. 3번의 테이블을 해커아이디, 이름으로 그룹핑 + 정렬
첫 오답
WITH main AS
(SELECT S.hacker_id as hacker_id,
S.score as score_01,
S.challenge_id as challenge_id,
H.name as name,
C.difficulty_level as difficulty_name,
D.score as score_02
FROM Submissions S
JOIN Hackers H ON H.hacker_id = S.hacker_id
JOIN Challenges C ON C.challenge_id = S.challenge_id
JOIN Difficulty D ON D.difficulty_level = C.difficulty_level)
SELECT hacker_id,
name,
count(challenge_id)
FROM main
WHERE score_01 = score_02
GROUP BY 1,2
ORDER BY count(challenge_id) DESC, 1
정답
WITH main AS (
SELECT
S.hacker_id AS hacker_id,
S.score AS score_01,
S.challenge_id AS challenge_id,
H.name AS name,
C.difficulty_level AS difficulty_name,
D.score AS score_02
FROM Submissions S
JOIN Hackers H ON H.hacker_id = S.hacker_id
JOIN Challenges C ON C.challenge_id = S.challenge_id
JOIN Difficulty D ON D.difficulty_level = C.difficulty_level
), main02 AS (
SELECT
hacker_id,
name,
COUNT(challenge_id) AS challenge_count
FROM main
WHERE score_01 = score_02 --- 조건 1 (전체 테이블)
GROUP BY hacker_id, name
HAVING COUNT(challenge_id) > 1 -- 조건 2 (그룹핑 된 후의 조건)
)
SELECT
hacker_id,
name
FROM main02
ORDER BY
challenge_count DESC,-- 정렬 조건 1
hacker_id -- 정렬 조건 2
궁금 한 점