select x.*,
	ceil(rnum / :lcnt) as pgno,
	max(rnum) over() as maxnum
from (
	select  z.*,
			ROW_NUMBER() over (order by z.cucd,z.jan) as rnum
	from (
		  select
                 a.cucd, a.cucd orgcucd,
                 c.cutax, 
                 c.cunm,
                 a.ijan, a.ijan orgijan,
                 b.jan, b.jan orgjan,
                 b.gdcls9,
                 d.scls5 as gdcls9nm,
                 b.gdjnm||' '||b.gdjstnd as gdjnm,
                 a.gdbcst, a.gdbcst orggdbcst,
                 a.gdsal, a.gdsal orggdsal,
                 a.gdbcst1, a.gdbcst1 orggdbcst1,
                 a.gdsal1, a.gdsal1 orggdsal1,
                 a.gdspcst, a.gdspcst orggdspcst,
                 to_char(a.gdspst, 'yy/mm/dd') as gdspst, to_char(a.gdspst, 'yy/mm/dd') as orggdspst,
                 to_char(a.gdspen, 'yy/mm/dd') as gdspen, to_char(a.gdspen, 'yy/mm/dd') as orggdspen,
                 a.cugdde, a.cugdde orgcugdde,
                 a.gdstun, a.gdstun orggdstun,
                 a.gdretcls, a.gdretcls orggdretcls,
                 decode(b.gdcls9,'1',-1,a.cufhcls) cufhcls,
                 decode(b.gdcls9,'1',-1,a.cufhcls) orgcufhcls,
                 to_char(a.cugdstp,'yyyy/mm/dd hh24:mi') cugdstp,
			     to_char(a.cugdstp,'yyyy/mm/dd hh24:mi') orgcugdstp
		 from afm90 d, afm03 c, afm02 b, afm0311 a
		 where a.ijan = b.ijan(+)
  		   and a.cucd = c.cucd(+)
  		   and d.cls(+) = '6' and b.gdcls9 = d.scls1(+)
  		   and (:cucd is null or a.cucd = :cucd)
  		   and (:brand is null or b.brand = :brand)
  		   and (:sjan is null or b.jan >= :sjan)
  		   and (:ejan is null or b.jan <= :ejan)
         and (:gstpflg <> 0 or b.gdcls9 <> '1')
		 order by a.cucd, b.jan
	) z
) x 