select alu.id_alu as CODIGO, ape_per as APELLIDO ,nom_per as NOMBRE ,des_cur as SECCION, alu.prom from (
select *
from (
select distinct cod_cur,  truncate(prom,5) as prom from( 
select s.cod_cur,id_alu,avg(round(nota_d,1)) as prom  from seg_final s ,periodo p,curso c,grado g
where p.per_not_fin='s' and per_fin='n' and p.id_peri=s.id_peri 
and c.cod_cur=s.cod_cur and g.cod_gra=c.cod_gra and cod_niv>1
group by s.cod_cur,id_alu
) p
order by  cod_cur,prom desc
) sel1
where (
   select count(*) from (
		select distinct cod_cur, truncate(prom,5) as prom from( 
		select s.cod_cur,id_alu,avg(round(nota_d,1)) as prom  from seg_final s ,periodo p,curso c,grado g
		where p.per_not_fin='s' and per_fin='n' and p.id_peri=s.id_peri 
		and c.cod_cur=s.cod_cur and g.cod_gra=c.cod_gra and cod_niv>1
		group by s.cod_cur,id_alu
		) p
		order by  cod_cur,prom desc
	) as f
   where f.cod_cur = sel1.cod_cur and f.prom >= sel1.prom 
) =2
order by cod_cur, sel1.prom desc
) pto 
inner join  (
select s.cod_cur,id_alu, truncate(avg(round(nota_d,1)),5) as prom  from seg_final s ,periodo p
where p.per_not_fin='s' and per_fin='n' and p.id_peri=s.id_peri 
group by s.cod_cur,id_alu
)alu on alu.cod_cur=pto.cod_cur and alu.prom=pto.prom
inner join alumno al on al.id_alu=alu.id_alu
inner join curso c on c.cod_cur=pto.cod_cur 