๐ข ๋ณธ ํฌ์คํ ์ ํ์ฉ๋๋ ๊ธฐ๋ณธ ๋ฌธ์ ๋ฐ ์๋ฃ ์ถ์ฒ๋ HackerRank ์์ ๋ฐํ๋๋ค.
โ ๋ฌธ์
You did such a great job helping Julia with her last coding contest challenge that she wants you to work on this one, too!
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 0 your result.
Input Format
The following tables contain contest data:
- Hackers: The hacker_id is the id of the hacker, and name is the name of the hacker.
- Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who made the submission, challenge_id is the id of the challenge for which the submission belongs to, and score is the score of the submission.
Sample Input
Hackers Table:
Submissions Table:
Sample Output
4071 Rose 191
74842 Lisa 174
84072 Bonnie 100
4806 Angela 89
26071 Frank 85
80305 Kimberly 67
49438 Patrick 43
Explanation
โ ํ์ด
๋ฌธ์ ์๊ตฌ์ฌํญ
> ์ด์ (total score) = ํด์ปค๋ณ ๊ทธ๋ค์ challenges์ ๋ํ ์ต๋ ์ ์์ ํฉ
> ์ ์ ๋ด๋ฆผ์ฐจ์์ผ๋ก hacker_id, name, and total score of the hackers ์ถ์ถ
(๋๋ช ์ด์์ ํด์ปค๊ฐ ์ด์ ์ด ๋๊ฐ๋ค๋ฉด, hacker_id ์์ผ๋ก ์ ๋ ฌ)
> ์ด์ ์ด 0์ธ ํด์ปค๋ค์ ๊ฒฐ๊ณผ์์ ์ ์ธํ ๊ฒ.
โ hackers ํ ์ด๋ธ๊ณผ Submissions ํ ์ด๋ธ์ ์กฐ์ธ
> ๋จ, explanation์ ๋ฐ๋ผ ํด์ปค๋ณ๋ก ๋์ผํ challege์ ๋ํ ์ด์ ์ '์ต๊ณ ์ ' ๋ง ๊ณ ๋ คํ๋ค.
โก Submissions ํ ์ด๋ธ์์ hacker_id, challenge_id๋ณ๋ก Max(score) max_score ์ ์ถ์ถ
โฒ ์ด ํ ์ด๋ธ๊ณผ Hackers ํ ์ด๋ธ์ inner join
ํด์ปค๋ณ id, name, total_score( sum(s.max_sore))๋ฅผ ์ถ์ถํ๋๋ฐ
โข sum(s.max_score)๊ฐ 0์ธ ํด์ปค๋ ์ ์ธํด์ผ ํ๋ฏ๋ก ์ง๊ณํจ์ ์กฐ๊ฑด์ธ HAVING์ ๋ก ์กฐ๊ฑด ๋ถ์ฌํ๊ณ
โฃ ์ ๋ ฌ (order by ~)
- (1์์) total_score๊ฐ ๋์ ์
- (2์์) total_score๊ฐ ๋์ผํ ๊ฒฝ์ฐ id ์ค๋ฆ์ฐจ์์ผ๋ก ์ ๋ ฌ
SELECT h.hacker_id, h.name, sum(s.max_score)
FROM Hackers h
JOIN (SELECT hacker_id, challenge_id, Max(score) max_score
FROM Submissions
GROUP BY hacker_id, challenge_id
) s
ON h.hacker_id = s.hacker_id
GROUP BY 1,2
HAVING SUM(s.max_score) > 0
ORDER BY SUM(s.max_score) DESC, h.hacker_id;
'Growth ๐ณ > Practice ๐ป' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[LeetCode] 511. Game Play Analysis I (0) | 2022.09.28 |
---|---|
[LeetCode] 586. Customer Placing the Largest Number of Orders (0) | 2022.09.28 |
[Hacker Rank] SQL Project Planning (0) | 2022.09.20 |
[Hacker Rank] Occupations (0) | 2022.09.20 |
[Hacker Rank] Draw The Triangle 2 (1) | 2022.09.20 |