Top x rows and group by (again)

Posted by Tibor Szasz on Stack Overflow See other posts from Stack Overflow or by Tibor Szasz
Published on 2010-05-05T19:51:30Z Indexed on 2010/05/05 19:58 UTC
Read the original article Hit count: 333

Filed under:
|

Hello,

I know it's a frequent question but I just can't figure it out and the examples I found didn't helped. What I learned, the best strategy is to try to find the top and bottom values of the top range and then select the rest, but implementing is a bit tricky.

Example table:

id | title | group_id | votes

I'd like to get the top 3 voted rows from the table, for each group.

I'm expecting this result:

91 | hello1 | 1 | 10 
28 | hello2 | 1 | 9
73 | hello3 | 1 | 8 
84 | hello4 | 2 | 456
58 | hello5 | 2 | 11 
56 | hello6 | 2 | 0 
17 | hello7 | 3 | 50
78 | hello8 | 3 | 9 
99 | hello9 | 3 | 1 

I've fond complex queries and examples, but they didn't really helped.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about group-by