티스토리 뷰
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 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.
이번 문제는 쉬운 문제인지.. 엄청 빨리 이해하고 풀었다. (소요시간 12분)
각 hacker_id별 total score를 구하는데 score는 각 challenge별 max값으로 계산해야 한다.
그래서 Submissions 테이블에서 hacker_id, challenge_id별 max(score) 값을 미리 계산한 다음,
Hackers 테이블과 join 하여 sum(max(score))를 구하도록 하였고 이 때 sum(max(score))값이 0인 것은 having절에서 제외하였다.
/*
SELECT hacker_id, name, total score of the hacker
FROM Hackers, Submissions
ORDER BY score DESC, hacker_id
WHERE Exclude all hackers with a total score of 0 from your result.
* total score of the hacker - sum of their maximum scores for all of the challenges
*/
SELECT A.hacker_id, A.name
, SUM(B.max_score)
FROM Hackers A
JOIN (
SELECT hacker_id, challenge_id, MAX(score) AS max_score
FROM Submissions
GROUP BY hacker_id, challenge_id
) B
ON A.hacker_id = B.hacker_id
GROUP BY A.hacker_id, A.name
HAVING SUM(B.max_score) > 0
ORDER BY SUM(B.max_score) DESC, A.hacker_id
11/16일 2차풀이 - 9분
'Programming > MySQL' 카테고리의 다른 글
[MySQL/Hackerrank] Placements (0) | 2021.11.06 |
---|---|
[MySQL/Hackerrank] SQL Project Planning (0) | 2021.11.06 |
[MySQL/Hackerrank] Challenges (0) | 2021.11.04 |
[MySQL/Hackerrank] Ollivander's Inventory (0) | 2021.11.03 |
[MySQL/Hackerrank] Top Competitors (0) | 2021.11.03 |
- Total
- Today
- Yesterday
- Weather Observation Station
- hackerrank
- 미중무역전쟁
- 분석탭
- 몰누피라비르
- Tableau
- insert
- 리비안
- MS SQL Server
- python3
- 해커랭크
- SQL Server
- string
- mysql
- TSQL
- 동국알앤에스
- 대원화성
- tensorflow
- HK이노엔
- 매매일지
- python
- 테슬라
- DATABASE
- 넥스트BT
- 에코캡
- MSSQL
- 경구치료제
- 코로나19
- list
- 넷플릭스
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |