SELECT
des_gra GRADO, des_cur SECCION , id_alu CODIGO
,ape_per APELLIDO ,nom_per NOMBRE ,des_mat ASIGNATURA ,round(nota_d,1) PROMEDIO, round(acumulado,1) as ACUMULADO,periodos_e PERIODOS_EVALUADOS

FROM 
(select id_alu,cod_cur,cod_mat, avg(nota_d) nota_d,(sum(nota_d)/(select count(*) from periodo where per_fin='n' and per_not_fin='n') )acumulado, count(nota_d) periodos_e from seg_final 
inner join periodo using(id_peri)
where cod_mat<>0 group by id_alu,cod_cur,cod_mat) s
inner join alumno using(id_alu)
inner join curso c using(cod_cur)
inner join materia using(cod_mat)
inner join grado g on(g.cod_gra= c.cod_gra) 
where 
est_alu='h' and
nota_d < (select min(min) from rangos_valora inner join tipvalora using(id_val) where val_apr='s')
or periodos_e < (select count(*) from periodo where per_cer='s' AND per_fin='n' and per_not_fin='n')
order by ord_gra,gru_cur,ape_per,des_mat