Ollivander's Inventory 문제 바로가기
Ollivander's Inventory | HackerRank
Help pick out Ron's new wand.
www.hackerrank.com
문제
Hermione decides the best way to choose is by determining the minimum number of gold galleons needed to buy each non-evil wand of high power and age. Write a query to print the id, age, coins_needed, and power of the wands that Ron's interested in, sorted in order of descending power. If more than one wand has same power, sort the result in order of descending age.
1. 론이 관심있을만한 wand 기준 : power, age는 높고, non-evil wand(is_evil = 0)이며 코인은 최소로 드는 wand
2. 론이 관심있을만한 wand의 id, age, coins_needed, power 를 결과값으로 나타내는 쿼리 입력
3. power 내림차순(정렬기준1), power같으면 age 내림차순(정렬기준2)
* wand_property 테이블에서 code와 age 매핑은 1:1이다 => 한 코드마다 나이 하나, 즉 동일 코드 = 동일 나이
문제 이해하는데 완전 오래걸림.....; 더 자세한 정보 및 예시는 위 문제링크에서 확인
풀이
SELECT w.id, p.age, w.coins_needed, w.power
FROM (SELECT code, power, MIN(coins_needed)coins_needed
FROM wands
GROUP BY code, power) sub -- code(age), power를 기준으로 최소 코인값만 저장
INNER JOIN wands_property p ON sub.code = p.code -- property 조인
INNER JOIN wands w ON sub.code = w.code
AND sub.coins_needed = w.coins_needed
AND sub.power = w.power -- id 속성을 위해 wands 다시 조인
WHERE p.is_evil = 0
ORDER BY power DESC, age DESC;
'SQL > MySQL 문제풀이' 카테고리의 다른 글
[HackerRank] Weather Observation Station 19 (풀이) (0) | 2022.06.11 |
---|---|
[HackerRank] SQL Project Planning Solution(풀이) & DATEDIFF (0) | 2022.06.01 |
[HackerRank] Challenges Solution(풀이방법) (0) | 2022.06.01 |
[HackerRank] Binary Tree Nodes Solution(풀이) (0) | 2022.06.01 |
[HackerRank] Contest Leaderboard Solution (풀이) (0) | 2022.05.29 |