코딩테스트/HackerRank

[Basic Join] Contest Leaderboard(Oracle)

봉두두 2022. 5. 17. 23:51
728x90
Level : SQL(Intermediate)

 

💡 Key note
Hacker의 total score란 모든 challenges의 max 점수에 대한 합임.

 

두 번의 삽질이 있었다.

첫째, 모든 challenges의 score를 더하는 줄 알았다가 실패.

둘째, challenges별 max 점수인데, partition by 시 엄한 submission_id로 잘못 이해해서 실패..(어휘력 무엇..)

 

사실 partition by도 필요가 없었다. max 함수를 활용해서 통과했다.

 

-- hacker_id, name, total score를 출력
-- 이 때 total score 가 높은 순으로 출력
-- 같은 total score인 hacker가 둘 이상일 경우 hacker_id순으로 출력
-- total score가 0인 hacker는 제외

select sub.hacker_id, sub.name, sum(sub.mx_score) total_score
from (
    select sm.challenge_id, sm.hacker_id, hc.name, max(sm.score) mx_score
    from Hackers hc right join submissions sm on hc.hacker_id=sm.hacker_id
    group by sm.challenge_id, sm.hacker_id, hc.name
) sub
having sum(sub.mx_score) <> 0
group by sub.hacker_id, sub.name
order by sum(sub.mx_score) desc, hacker_id;

 

728x90
728x90

'코딩테스트 > HackerRank' 카테고리의 다른 글

[SQL] order by substr (Oracle)  (0) 2022.06.28
[Advanced Select] Type of Triangle  (0) 2022.06.26
[Basic Join] Top Competitors(Oracle)  (0) 2022.05.03
[Basic Join] The Report(Oracle)  (0) 2022.05.03
[Advanced select] The PADS(Oracle)  (0) 2022.04.22