select b.id_alu as CODIGO,ape_per as APELLIDO,nom_per as NOMBRE,des_cur as SECCION ,count(nota) as TOTAL from (

select id_alu,cod_cur,cod_mat,round(nota_d,1) as nota
from seg_final a 
inner  join rangos_valora r on nota>=min and nota<=max
inner join tipvalora v on v.id_val=r.id_val and v.val_apr='n'
inner join periodo p on p.id_peri=a.id_peri and per_not_fin='s'
)b 
inner join alumno l using(id_alu)
inner join curso c using (cod_cur)
inner join grado g using (cod_gra)
where est_alu='h' and cod_gra  = VAR01
group by id_alu,cod_cur 
order by cod_cur, TOTAL desc