728x90
※ The following tables contain data on the wands in Ollivander's inventory:
- Wands: The id is the id of the wand, code is the code of the wand, coins_needed is the total number of gold galleons needed to buy the wand, and power denotes the quality of the wand (the higher the power, the better the wand is).
- Wands_Property: The code is the code of the wand, age is the age of the wand, and is_evil denotes whether the wand is good for the dark arts. If the value of is_evil is 0, it means that the wand is not evil. The mapping between code and age is one-one, meaning that if there are two pairs, (code1, age1) and (code2, age2), then code1 ≠ code2 and age1 ≠ age2.
■ [Basic Join] Ollivander's Inventory
https://www.hackerrank.com/challenges/harry-potter-and-wands/problem?isFullScreen=true
Q.
Harry Potter and his friends are at Ollivander's with Ron, finally replacing Charlie's old broken wand.
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.
A.
WITH WWP AS (
SELECT ID, W.CODE AS CODE, COINS_NEEDED, POWER, AGE, IS_EVIL
FROM WANDS W, WANDS_PROPERTY WP
WHERE W.CODE = WP.CODE
)
SELECT ID, G_AGE, MIN_COINS_NEEDED, G_POWER
FROM WWP JOIN (
SELECT AGE AS G_AGE, POWER AS G_POWER, MIN(COINS_NEEDED) AS MIN_COINS_NEEDED
FROM WWP
WHERE IS_EVIL = 0
GROUP BY AGE, POWER
) WWP_FL
ON WWP.AGE = WWP_FL.G_AGE AND
WWP.POWER = WWP_FL.G_POWER AND
WWP.COINS_NEEDED = WWP_FL.MIN_COINS_NEEDED
ORDER BY G_POWER DESC, G_AGE DESC;
SELECT ID, G_AGE, MIN_COINS_NEEDED, G_POWER
FROM WANDS W JOIN (
SELECT W.CODE, AGE AS G_AGE, POWER AS G_POWER, MIN(COINS_NEEDED) AS MIN_COINS_NEEDED
FROM WANDS W JOIN WANDS_PROPERTY WP ON W.CODE = WP.CODE
WHERE IS_EVIL = 0
GROUP BY W.CODE, AGE, POWER
) WWP_FL
ON W.CODE = WWP_FL.CODE AND
W.POWER = WWP_FL.G_POWER AND
W.COINS_NEEDED = WWP_FL.MIN_COINS_NEEDED
ORDER BY G_POWER DESC, G_AGE DESC;
반응형
'코딩 문제 풀기 ( Algorithm problem solving ) > 해커랭크 ( HackerRank )' 카테고리의 다른 글
[HackerRank][SQL(Oracle)] Challenges (0) | 2023.04.01 |
---|---|
[HackerRank][SQL(Oracle)] The PADS (0) | 2023.04.01 |
[HackerRank][SQL(Oracle)] SQL Project Planning (0) | 2023.03.29 |
[HackerRank][SQL(Oracle)] Print Prime Numbers (0) | 2023.03.29 |
[HackerRank][SQL(Oracle)] Symmetric Pairs (0) | 2023.03.29 |