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
|
1877 Discussions
|
Please Login in order to post a comment
with min_coin as ( select code, power, min(coins_needed) as coins_required from Wands group by code, power )
Select w.id, wp.age, w.coins_needed, w.power from Wands w join Wands_property wp on w.code = wp.code join min_coin mc on w.code = mc.code and w.power = mc.power where wp.is_evil = 0 and w.coins_needed = mc.coins_required order by w.power desc, wp.age desc
ngoccth_SQL SERVER: WITH table_1 AS ( SELECT id, age, MIN(coins_needed) AS Min, [power] , row_number () OVER (PARTITION BY [power], age ORDER BY MIN(coins_needed)) AS num FROM Wands LEFT JOIN Wands_Property ON Wands.code = Wands_Property.code WHERE is_evil = 0 GROUP BY id, age, [power] ) SELECT id, age, Min, [power] FROM table_1 WHERE num = 1 ORDER BY power DESC, age DESC
WITH rankedWands AS ( SELECT W.id, W.code, WP.age, W.coins_needed, W.power, RANK() OVER (PARTITION BY W.code, WP.age ,w.power ORDER BY W.coins_needed ASC) AS rank FROM Wands AS W INNER JOIN Wands_Property AS WP ON W.code = WP.code WHERE WP.is_evil = 0 ) SELECT id, age, coins_needed, power FROM rankedWands WHERE rank = 1 -- Selects the rows with the minimum coins_needed per code and age ORDER BY power DESC, age DESC;
-- query to print id,age,coins_need and power of the wands sorted by order of descending power -- if power is same sort the results by descending age --lets get a CTE for minimum coins needed WITH coins_min AS ( SELECT code, power, MIN(coins_needed) AS minimum_coins FROM wands GROUP BY code, power ) SELECT w.id, p.age, w.coins_needed, w.power FROM wands w JOIN wands_property p ON w.code = p.code JOIN coins_min c ON w.code = c.code AND w.power = c.power WHERE p.is_evil = 0 AND w.coins_needed = c.minimum_coins ORDER BY w.power DESC, p.age DESC;