I couldn't frame the question's title properly. Suppose a table of weekly movie earnings as below:
MovieName <Varchar(450)>
MovieGross <Decimal(18)>
WeekofYear <Integer>
Year <Integer>
So how do I get the names of top grossers for each week of this year, if I do:
select MovieName , Max(MovieGross) , WeekofYear
from earnings where year = 2010 group by WeekofYear;
Then obviously the query wont run, while
select Max(MovieName) , Max(MovieGross) , WeekofYear
from earnings where year = 2010 group by WeekofYear;
would just give movies starting with lowest alphabet. Is using group_concat()
and then substring_index()
the only option here?
select
substring_index(group_concat(MovieName order by MovieGross desc),',',1),
Max(MovieGross) , WeekofYear from earnings where year = 2010
group by WeekofYear ;
Seems clumsy. Is there any better way of achieving this?