In this HackerRank Ollivander's Inventory problem solution, 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.
Problem solution MS SQL.
select w.id, wp.age, w.coins_needed, w.power from wands w inner join wands_property wp on wp.code = w.code inner join ( select wp.age, min(w.coins_needed) 'coins_needed', w.power from wands w inner join wands_property wp on wp.code = w.code where wp.is_evil = 0 group by wp.age, w.power ) x on x.age = wp.age and x.coins_needed = w.coins_needed and x.power = w.power where wp.is_evil = 0 order by w.power desc, wp.age desc
Problem solution in Oracle.
/* Enter your query here. Please append a semicolon ";" at the end of the query and enter your query in a single line to avoid error. */ With X as (select w.id i, p.age a, w.coins_needed c, w.power p from wands w, wands_property p where w.code = p.code and p.is_evil = 0), Y as (select p.age a, min(w.coins_needed) c, w.power p from wands w, wands_property p where w.code = p.code and p.is_evil = 0 group by w.power, p.age) select X.i, X.a, X.c, X.p from X,Y where X.c = Y.c and X.p = Y.p and X.a = Y.a order by X.p desc, X.a desc, X.a;
Problem solution in DB2.
SELECT W.ID, WP.AGE, W.COINS_NEEDED, W.POWER FROM WANDS W INNER JOIN WANDS_PROPERTY WP ON WP.CODE = W.CODE INNER JOIN ( SELECT MIN(W.COINS_NEEDED) AS COINS_NEEDED, W.POWER, WP.AGE FROM WANDS W INNER JOIN WANDS_PROPERTY WP ON WP.CODE = W.CODE WHERE WP.IS_EVIL = 0 GROUP BY POWER, AGE ) WM ON WM.COINS_NEEDED = W.COINS_NEEDED AND WM.POWER = W.POWER AND WM.AGE = WP.AGE WHERE WP.IS_EVIL = 0 ORDER BY W.POWER DESC, WP.AGE DESC;
0 Comments