I'm trying to get the TOP X results for a given GROUP BY condition. I'm currently using something like this:
SELECT * FROM 
        (SELECT id  
            FROM myTable 
            WHERE id IN (x1, x2, ..., xn) GROUP BY id ORDER BY grade DESC 
        ) t1 
        INNER JOIN myTable t2 ON t2.id=t1.id
id is a non-unique INT indexed field, with multiple rows per value.
This returns me for each id, the row with the best grade. How can I convert this to return the TOP X results for each id?
For example, for the following data
id  grade
2   10
2   13
2   15
3   20
4   16
4   55
4   45
4   35
4   25
5   1
assuming X from TOP X is 2, I would like to get the rows of:
id grade  
2  15  
2  13  
3  20  
4  50  
4  40  
5  1  
 
     
     
    