MySQL - how to retrieve columns in same row as the values returned by min/mx

Posted by Gala101 on Stack Overflow See other posts from Stack Overflow or by Gala101
Published on 2010-05-13T16:29:35Z Indexed on 2010/05/13 16:34 UTC
Read the original article Hit count: 220

I couldn't frame the Question's title properly..

Suppose a table of weekly movie Earnings as below, MovieName MovieGross WeekofYear Year

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 query wont run,

    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 acieveing this?

© Stack Overflow or respective owner

Related posts about mysql

Related posts about aggregate