hackerrank_Contest Leaderboard

이지윤·2023년 3월 24일
0

comeonsql

목록 보기
1/2

The total score of a hacker is the sum of their maximum scores for all of the challenges. Write a query to print the hacker_id, name, and total score of the hackers ordered by the descending score. If more than one hacker achieved the same total score, then sort the result by ascending hacker_id. Exclude all hackers with a total score of from your result
"total score는 모든 문제의 최대값을 합한 값이다.
hacker_id, name, total score를 출력하는 쿼리를 입력하고
total socore를 기준으로 내림차순하라 만약 total score가 같다면 hacker id로 오름차순하라
total score의 점수가 0인 경우는 제외하라

이 문제의 관건은
한 학생이 같은 문제를 여러번 풀었을때 max값을 어떻게 찾아내는지가 중요

-- hacker_id, name, total_score
SELECT sub2.hacker_id, hackers.name, sub2.total_score
FROM (
      SELECT sub.hacker_id, SUM(max_score) AS total_score
      FROM (
            SELECT hacker_id, challenge_id, MAX(score) AS max_score
            FROM submissions
            GROUP BY hacker_id, challenge_id
            ) sub
      GROUP BY sub.hacker_id
      HAVING total_score > 0
) sub2 
  INNER JOIN hackers ON sub2.hacker_id = hackers.hacker_id
ORDER BY sub2.total_score DESC, sub2.hacker_id코드를 입력하세요
SELECT hacker_id, challenge_id, MAX(score) AS max_score
            FROM submissions
            GROUP BY hacker_id, challenge_id
-- hacker_id, challenge_id 두개로 그룹바이 해주면 각 문제의 max_socore를 알수 있다.
SELECT sub.hacker_id, SUM(max_score) AS total_score
      FROM (
            SELECT hacker_id, challenge_id, MAX(score) AS max_score
            FROM submissions
            GROUP BY hacker_id, challenge_id
            ) sub
      GROUP BY sub.hacker_id
      HAVING total_score > 0

-- 우리가 필요한건 hacker_id, name, total_score이므로 challeneg_id는 제외하고
-- 다시 묶어주면서 total_score가 0이 아닌애들을 조건으로 걸어준다
SELECT sub.hacker_id, SUM(max_score) AS total_score
      FROM (
            SELECT hacker_id, challenge_id, MAX(score) AS max_score
            FROM submissions
            GROUP BY hacker_id, challenge_id
            ) sub
      GROUP BY sub.hacker_id
      HAVING total_score > 0

-- 우리가 필요한건 hacker_id, name, total_score이므로 challeneg_id는 제외하고
-- 다시 묶어주면서 total_score가 0이 아닌애들을 조건으로 걸어준다
profile
뽀개버려 데이터 분석

0개의 댓글