Ollivander's Inventory

Sort by

recency

|

1961 Discussions

|

  • + 0 comments

    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;

  • + 0 comments

    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;

  • + 0 comments

    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;

  • + 0 comments

    If you can use Window Functions then do use them

    /*
    Hint
    
    1. In the outer query, I am selecting columns from the subquery where Rank = 1. Therefore Rank() alias is used explicitly. Also, Rank is a reserved keyword in SQL, so it is a good practice to use double quotes ("Rank") or rename it (rank_col).
    
    2. ORDER BY Clause in RANK() Should Match the PARTITION BY Columns
    
        PARTITION BY W.power, WP.age suggests ranking within each (power, age) combination. My intention is to find the wand with the lowest coins_needed for each (power, age), so order by coins_needed ASC.  Note:  ORDER BY W.power DESC, WP.age DESC sorts all rows.
    
    */
    
    SELECT id, age, coins_needed, power 
    FROM (
        SELECT 
            W.id, 
            W.code, 
            W.coins_needed, 
            W.power,
            WP.age,
            RANK() OVER (PARTITION BY W.power, WP.age 
                         ORDER BY W.coins_needed ASC) AS rank_col
        FROM Wands W 
        JOIN Wands_Property WP ON W.code = WP.code  
        WHERE WP.is_evil = 0
    ) sub 
    WHERE rank_col = 1
    ORDER BY power DESC, age DESC;
    
  • + 0 comments

    Not the most elegant solution, but it does the job and I'm proud of it.

    WITH MW AS (SELECT power, age, MIN(coins_needed) AS min_coins FROM Wands JOIN Wands_Property USING(code) GROUP BY age, power), W AS (SELECT id, age, coins_needed, power FROM Wands JOIN Wands_Property USING(code) WHERE is_evil = 0) SELECT id, age, coins_needed, power FROM W WHERE coins_needed = (SELECT min_coins FROM MW WHERE MW.power = W.power AND MW.age = W.age) ORDER BY power DESC, age DESC;
    

    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.