We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Prepare
- SQL
- Basic Join
- Ollivander's Inventory
- Discussions
Ollivander's Inventory
Ollivander's Inventory
Sort by
recency
|
1961 Discussions
|
Please Login in order to post a comment
MYSQL
select id,c.age as age,c.coins_needed as coins_needed,c.power as power from (select id, coins_needed,power, age from (select id,code,coins_needed,power from wands ) a join (select code,age,is_evil from wands_property ) b on a.code = b.code where is_evil = 0 ) c join (select power, age, min(coins_needed) as min_coins from (select id,code,coins_needed,power from wands ) a join (select code,age,is_evil from Wands_Property where is_evil = 0 ) b on a.code=b.code group by power,age) d on c.power=d.power and c.age=d.age and c.coins_needed=d.min_coins order by c.power Desc, c.age desc;
SELECT w.id, wp.age, w.coins_needed, w.power FROM Wands w JOIN Wands_Property wp ON w.code = wp.code WHERE wp.is_evil = 0 AND w.coins_needed = ( SELECT MIN(w1.coins_needed) FROM Wands w1 WHERE w1.power = w.power AND w1.code = w.code ) ORDER BY w.power DESC, wp.age DESC;
SELECT ID, AGE, COINS_NEEDED, POWER FROM ( SELECT ID, AGE, COINS_NEEDED, POWER, ROW_NUMBER() OVER (PARTITION BY AGE, POWER ORDER BY COINS_NEEDED ASC) AS RN FROM ( SELECT W.ID, WP.AGE, W.COINS_NEEDED, W.POWER FROM Wands W JOIN Wands_Property WP ON WP.CODE = W.CODE WHERE WP.IS_EVIL != 1 ) )
WHERE RN = 1 ORDER BY POWER DESC, AGE DESC;
If you can use Window Functions then do use them
Not the most elegant solution, but it does the job and I'm proud of it.
Also I hated this question, as it was horribly redacted and I had to come to the comments to figure out what the query was even supposed to be.