select x.*,
	ceil(rnum / :lcnt) as pgno,
	0 chkval,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY z.ordkey1, z.ordkey2, z.ordkey3) as rnum,
		z.*,
		case
			when nvl(z.amt01,0)=0 then
				case when nvl(z.amt02, 0) = 0 then 0 else 999.99 end
			else abs(round(z.amt02/nvl(z.amt01,0)*100,2))
		end rt02, /*返品率*/
		case
			when nvl(z.amt01,0)=0 then
				case when nvl(z.amt03,0) = 0 then null else 999.99 end
			else abs(round(nvl(z.amt03,0)/nvl(z.amt01,0)*100,2))
		end rt03, /*値引率*/
		decode(nvl(z.amt,0), 0, 999.99, abs(round(nvl(z.ar,0)/nvl(z.amt,0)*100,2))) as rtar, /*粗利率*/
		decode(nvl(z.tamt02,0), 0, 0, decode(nvl(z.tamt01,0), 0, 0, abs(round(nvl(z.tamt02,0)/nvl(z.tamt01,0)*100,2)))) as trt02, /*合計返品率*/
		decode(nvl(z.tamt03,0), 0, 0, decode(nvl(z.tamt01,0), 0, 0, abs(round(nvl(z.tamt03,0)/nvl(z.tamt01,0)*100,2)))) as trt03, /*合計値引率*/
		decode(nvl(z.tar,0), 0, 0, decode(nvl(z.tamt,0), 0, 0, round(nvl(z.tar,0)/nvl(z.tamt,0)*100,2))) as trtar /*合計粗利率*/
	from (
		select
			a.cucd,
			a.cuscd,
			b.cunm,
			c.spnm,
			to_char(decode(d.cls,0,a.adat,null),'yyyy/mm/dd') as adat,
			sum(decode(a.tcls,'01',a.amt)) as amt01,
			nvl(sum(decode(a.tcls,'02',a.amt)),0) as amt02,
			sum(decode(a.tcls,'03',a.amt)) as amt03,
			sum(a.amt) as amt,
			sum(a.amt-calc_amt(null,a.tpno,a.fcst,2,a.ktktcd,z.scutaxc)) as ar,
			sum(nvl(sum(decode(a.tcls,'01',a.amt)),0)) over() as tamt01,
			sum(nvl(sum(decode(a.tcls,'02',a.amt)),0)) over() as tamt02,
			sum(nvl(sum(decode(a.tcls,'03',a.amt)),0)) over() as tamt03,
			sum(sum(a.amt)) over() as tamt,
			sum(nvl(sum(a.amt-calc_amt(null,a.tpno,a.fcst,2,a.ktktcd,z.scutaxc)),0)) over() as tar,
			decode(:rdo_ord, '0', to_char(decode(d.cls,0,a.adat,null),'yyyy/mm/dd'), a.cucd) as ordkey1,
			decode(:rdo_ord, '0', a.cucd, a.cuscd) as ordkey2,
			decode(:rdo_ord, '0', a.cuscd, to_char(decode(d.cls,0,a.adat,null),'yyyy/mm/dd')) as ordkey3
