select x.*,
	ceil(rnum / :lcnt) as pgno,
	0 chkval,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY nvl(a.ndat,to_date('19000101','yyyymmdd')) desc,a.dat,a.dat1,a.vseq,a.spktcd) as rnum,
		a.clsnm1,
		to_char(a.dat1,'yyyy/mm/dd') dat1,
		a.vseq,
		to_char(a.dat,'yyyy/mm/dd') dat,
		a.pno,
		to_char(a.ndat,'yyyy/mm/dd') ndat,
		a.jpno,
		a.chgnm,
		a.spktnm2,
		decode(a.spktnm2,null,'','→') lblspktnm2,
		a.spktcd,
		a.spktnm,
		a.com,
		(case when nvl(a.sts,'NULL') in (' ','X') and nvl(a.pno,0)>nvl(a.jpno,0) then '残あり' else null end) sts_nm
	from af291v02 a
	where a.ktktcd=:ktktcd
		and (:spktcd is null or a.spktcd=:spktcd)
		and a.ijan=:ijan
		and (:ndats is null or a.ndat>=to_date(:ndats,'yyyy/mm/dd'))
		and (:ndate is null or a.ndat<=to_date(:ndate,'yyyy/mm/dd'))
		and (nvl(:rdo_ceslip2,'0')='0' or a.cls=:rdo_ceslip2)
) x
order by x.rnum
