select x.*,
	ceil(rnum / :lcnt) as pgno,
	0 chkval,
	max(rnum) over() as maxnum
from (
	select
			ROW_NUMBER() OVER ( ORDER BY a.cucd,a.cuscd) as rnum,
			a.cucd,
			a.cuscd,
			a.cunm||' '||a.spnm cunm,
			a.cudt,
			to_char(a.cdat,'yyyy/mm/dd') cdat,
			a.amt,
			to_char(a.spri,'yyyy/mm/dd hh24:mi:ss') spri,
			a.sptyp,
			1 cpnum,
			decode(a.sts,'2','済','1','未','3','解除') stsnm,
			decode(Chk_Limit(a.ktktcd,a.cucd,a.cuscd,a.sbat,a.cdat),0,null,'●') lchk,
			a.sbat,
			a.typ,
			to_char(a.pdat,'yyyy/mm/dd') pdat,
			a.pamt,
			a.cuedi
	from af461v02 a
	where a.ktktcd = :ktktcd
		and a.cudt = :cudt
		and a.cdat = to_date(:cdat,'yyyy/mm/dd')
		and (:cucd is null or a.cucd=get_cumcd(:cucd,'2'))
		and (nvl(:head_chk,'0')='1' or a.typ is not null ) /*請求書種類無*/
		and (:rdo_cls = '0' 
			or (:rdo_cls = '2' and a.spsts = -1) /*発行済*/
			or (:rdo_cls = '1' and (chk_limit(a.ktktcd,a.cucd,a.cuscd,a.sbat,a.cdat)<>0 or spsts = 0)) ) /*未発行又は要確認*/
		and (nvl(:rdo_cuspflg,'2')='2' or a.cuspflg=nvl(:rdo_cuspflg,'2')) /*特別対応*/
		and (nvl(:rdo_cuosflg,'2')='2' or a.cuosflg=nvl(:rdo_cuosflg,'2')) /*オリジナル請求書*/
		and (:rdo_cls2 = '9'
			or (:rdo_cls2 = '2' and a.sts = '2')  /*締済*/
			or (:rdo_cls2 = '1' and a.sts = '1')  /*未締*/
			or (:rdo_cls2 = '3' and a.sts = '3')) /*締解除*/
) x
order by x.rnum
