Alternative to partial(nonaggregated column) in group by [Ollivander's Inventory problem on hackerrank]

倖福魔咒の 提交于 2021-01-29 14:34:22

问题


I am trying to solve https://www.hackerrank.com/challenges/harry-potter-and-wands/problem

With proper mysql setup one can do following

select w.id, wp.age, min(w.coins_needed), w.power from wands w
join wands_property wp
on wp.code = w.code and wp.is_evil=0
group by w.code
order by w.power desc, wp.age desc;

But hackerrank's mysql setup does not allow partial grouping. It throws error

SELECT list is not in GROUP BY clause and contains nonaggregated column 'run_eootvjd0lna.w.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

So I would like to first filter out all the duplicates for a given code in wands table and only leave rows having minimum coins_needed. This way I can then join wands_property on the extracted table.

Would like to get suggestion on extraction of only relevant minimum coins_needed rows, so that it can be used further on.

PS: I have created a sqlfiddle with sample data here


回答1:


I am not sure if i got it right

But with

select w.id, w.code, w2.coins_needed, w.power
, wp.age 
from wands w
inner join (SELECT code, Min(coins_needed) coins_needed From Wands Group by code)  w2 
on w.code = w2.code and w.coins_needed = w2.coins_needed
join wands_property wp
on wp.code = w.code and wp.is_evil=0

You get

id  code    coins_needed    power   age
1   10      100             2       100
3   20      300             3       200
8   40      300             4       400

This is the result for all wands that cost minimum for every code that is not evil with out grouping it.




回答2:


Here is my Explaination of the solution (final solution in step 3):

Step 1:

Filter rows with minimum coins needed based on power & code. Why code & power because requirement wants us to filter based on age and power and age has 1-1 relationship with code so code and power are logical places to filter

select w2.code, w2.power, min(w2.coins_needed) coins
from wands w2
group by w2.code, w2.power

Step 2:

Use this table with only filtered rows to get all the columns of wands table

select w.id, wp.age, w.code, min(w.coins_needed), w.power  from wands w
join (
        select w2.code, w2.power, min(w2.coins_needed) coins from wands w2
        group by w2.code, w2.power
      ) wcp
on wcp.code = w.code and wcp.power = w.power and wcp.coins = w.coins_needed

Step 3:

Final step is to join with wands_property with the new table we got in step 2 and order it as required by the problem

select w.id, wp.age, w.code, min(w.coins_needed), w.power  from wands w
join (
        select w2.code, w2.power, min(w2.coins_needed) coins from wands w2
        group by w2.code, w2.power
      ) wcp
on wcp.code = w.code and wcp.power = w.power and wcp.coins = w.coins_needed
join wands_property wp
on wp.code = w.code and wp.is_evil=0
order by w.power desc, wp.age desc ;


来源:https://stackoverflow.com/questions/58329024/alternative-to-partialnonaggregated-column-in-group-by-ollivanders-inventory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!