select x.*,
	ceil(rnum / :lcnt) as pgno,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY cucd,cuscd,adat,dcls,vseq,vlin) as rnum,
		to_char(a.adat,'yyyy/mm/dd') adat,
		a.vseq,
		a.vlin,
		a.cls,
		a.cucd,
		a.cunm,
		a.cuscd,
		a.spnm,
		a.jan,
		a.jpno,
		a.tpno,
		a.ncst,
		a.fcst,
		a.jamt,
		a.uamt,
		a.namt,
		a.txt2 com,
		a.spktcd,
		a.spktnm,
		a.jmcatnm,
		a.cshcd,
		to_char(a.dat1,'yyyy/mm/dd') dat1,
		a.dcls,
		sum(a.jpno) over() as tjpno,
		sum(a.tpno) over() as ttpno,
		sum(a.jamt) over() as tjamt,
		sum(a.uamt) over() as tuamt,
		sum(a.namt) over() as tnamt
	from af282v03 a
	where a.ktktcd=:ktktcd
		and a.adat>=to_date(:adats,'yyyy/mm/dd')
		and a.adat<=to_date(:adate,'yyyy/mm/dd')
		and (:dat1s is null or a.dat1>=to_date(:dat1s,'yyyy/mm/dd'))
		and (:dat1e is null or a.dat1<=to_date(:dat1e,'yyyy/mm/dd'))
		and (:cucd is null or a.cucd=:cucd)
		and (:cuscd is null or a.cuscd=:cuscd)
		and (:vseq is null or a.vseq=:vseq)
		and (:viseq is null or a.viseq=:viseq)
		and (:jans is null or a.jan>=:jans)
		and (:jane is null or a.jan<=:jane)
		and (	(nvl(:cls,'000') = '000') /*全て*/
			or
			(:cls = '101'  and dcls='1' and tcls='01') /*売上(全て)*/
			or
			(:cls = '1011' and dcls='1' and tcls='01' and af1='1') /*売上(EOS)*/
			or
			(:cls = '1012' and dcls='1' and tcls='01' and af1='2' and jmcat<>'2') /*売上(FAX) (通常)*/
			or
			(:cls = '1012' and dcls='1' and tcls='01' and af1='2' and jmcat='2') /*売上(FAX) (販促)*/
			or
			(:cls = '1013' and dcls='1' and tcls='01' and af1='3') /*売上(直送)*/
			or
			(:cls = '1014' and dcls='1' and tcls='01' and af1='4') /*売上*/
			or
			(:cls = '102'  and dcls='1' and tcls='02') /*返品(全て)*/
			or
			(:cls = '1021' and dcls='1' and tcls='02' and piseq is not null) /*返品(EOS)*/
			or
			(:cls = '1022' and dcls='1' and tcls='02' and piseq is null) /*返品(手入力)*/
			or
			(:cls = '103'  and dcls='1' and tcls='03') /*値引*/
			or
			(:cls = '299'  and dcls='2') /*入金*/
			)
		and (:chgcd is null or a.cuslcd=:chgcd)
		and (:spktcd is null or a.spktcd=:spktcd)
) x
order by x.rnum
