Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

Obtendo a média dos 10 melhores alunos de cada escola


Usando esta técnica .
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