select x.*,
    ceil(rnum / :lcnt) as pgno,
    max(rnum) over() as maxnum
from (
	select z.*,
	    ROW_NUMBER() over (order by z.cucd,z.medcd,z.cugdcd) as rnum
	from (
		select 
			a.cucd,a.cuchgcd,a.gdflg,
			a.cugdcd,
			a.medcd,
			a.cugdde,
			a.cvsno,
       		a.ijan,
			b.jan,b.gdjnm||' '||b.gdjstnd gdnm,c.scls5
		from afm90 c,afm02 b,afm031 a
		where a.cucd = :cucd
  		  and a.gdflg='1'
		  and a.ijan=b.ijan(+)
  		  and b.gdcls9=c.scls1(+) and c.cls(+)='6' 
  		  and (:cugdcdst is null or a.cugdcd >= :cugdcdst)
  		  and (:cugdcden is null or a.cugdcd <= :cugdcden)
		order by a.cugdcd
	) z
) x