select x.*,
	ceil(rnum / :lcnt) as pgno,
	0 chkval,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY c.jan) as rnum,
		/*ヘッダー用*/
		decode(e.modfr,0,'FAX発注',-1,'ﾊｳﾈｯﾄ',-2,'プラネット','設定なし') modfr,
		e.htpnt1 htpnt1h,e.htpnt2 htpnt2h,e.htlmt1,e.htlmt2,
		/*明細用*/
		c.jan,a.ijan,d.scls5 gdcls9nm,c.gdjnm,c.gdjstnd,
		c.gdcnum,a.htcls,a.htpcls,c.gdhtun,
		a.htpnt1,a.htpnt2,a.fhtpnt1,a.fhtpnt2,
		f.jh02,f.jh03,f.jh04,f.jh05,f.jh06,f.jh07,f.jh08,f.jh09,
		/*変更有無チェック用*/
		a.htcls   old_htcls  , 
		a.htpcls  old_htpcls , 
		c.gdhtun  old_gdhtun , 
		a.htpnt1  old_htpnt1 , 
		a.htpnt2  old_htpnt2 , 
		a.fhtpnt1 old_fhtpnt1, 
		a.fhtpnt2 old_fhtpnt2
	from afs741 f,afm04 e,afm90 d,afm02 c,afm02e b,afm02d a
	where a.ktktcd=:ktktcd
		and a.ijan=b.ijan and b.mcd=:mcd
		and a.ijan=c.ijan
		and (:jans is null or (:jans is not null and c.jan>=:jans))
		and (:jane is null or (:jane is not null and c.jan<=:jane))
		and d.cls(+)='6' and c.gdcls9=d.scls1(+)
		and b.mcd=e.mcd(+) and a.ijan=f.ijan(+)
		and f.ktktcd(+)=:pktktcd
) x
order by x.rnum
