select x.*,
	ceil(rnum / :lcnt) as pgno,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY nvl(a.err,'999'),a.cuscd,a.vseq) as rnum,
		a.cuscd,
		a.cuedi,
		a.cuspcd,
		a.amt,
		a.vseq,
		to_char(a.mdat,'yyyy/mm/dd') mdat,
		b.msg,
		a.cucd,
		a.cuslip
	from afm91 b,afw63 a
	where a.err = b.err(+) and a.obat=:obat and a.rcvdst=:rcvdst
	  and nvl(a.cucd,'NULL') = nvl(:cucd,'NULL')
) x
order by x.rnum
