select x.*,
	ceil(rnum / :lcnt) as pgno,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY adat,vseq) as rnum,
		to_char(a.adat,'yyyy/mm/dd') adat,
		a.mcd,
		a.mnm,
		a.vseq,
		a.viseq,
		a.cls,
		a.spktcd,
		a.spktnm,
		a.hvseq,
		sum(samt) samt,
		sum(a.shamt) shamt,
		sum(sum(a.samt)) over() as tamt01,
		sum(sum(a.shamt)) over() as tamt02
	from af283v02 a
	where a.ktktcd=:ktktcd
		and a.adat>=to_date(:adats,'yyyy/mm/dd')
		and a.adat<=to_date(:adate,'yyyy/mm/dd')
		and (:mcd is null or a.mcd=:mcd)
		and (	(nvl(:cls,'000') = '000') /*全て*/
			or
			(:cls = '101'  and dcls='3' and tcls in ('11','31')) /*仕入*/
			or
			(:cls = '102'  and dcls='3' and tcls='21') /*返品*/
			or
			(:cls = '103'  and dcls='3' and tcls='13') /*値引*/
			or
			(:cls = '299'  and dcls='4') /*入金*/
			)
	group by mcd,mnm,adat,vseq,viseq,dcls,cls,viseq,spktcd,spktnm,hvseq
) x
order by x.rnum
