mysql select top n rows per group

Solutions on MaxInterview for mysql select top n rows per group by the best coders in the world

showing results for - "mysql select top n rows per group"
Lukas
22 Aug 2019
1CREATE TABLE yourtable (
2  year int,
3  id varchar(20),
4  rate decimal(10,2)
5);
6
7SELECT yourtable.*
8FROM   yourtable 
9INNER JOIN (
10  SELECT   id, GROUP_CONCAT(year ORDER BY rate DESC) grouped_year
11  FROM     yourtable
12  GROUP BY id) group_max
13ON yourtable.id = group_max.id AND FIND_IN_SET(year, grouped_year) <=5
14ORDER BY yourtable.id, yourtable.year DESC;