Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Abrufen des Durchschnitts der 10 besten Schüler jeder Schule

Mit dieser Technik .

select sch_code,
       schabbrev,
       ROUND( AVG( totpct_stu ), 1 ) AS top10
from   (select sch_code,
               schabbrev,
               totpct_stu,
               @num := if(@group = sch_code, @num + 1, 1) as row_number,
               @group := sch_code as dummy
        from   test_table
        order by sch_code, totpct_stu desc) as x
where  row_number <= 10
GROUP BY sch_code,
       schabbrev