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) 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(z.amt03/nvl(z.amt01,0)*100,2))
		end rt03, /*値引率*/
		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  /*合計値引率*/
	from (
		select
			a.mcd,
			b.mnm,
			to_char(decode(d.cls,0,a.adat,null),'yyyy/mm/dd') as adat,
			sum(decode(a.tcls,'11',a.amt,'31',a.amt)) as amt01,
			sum(decode(a.tcls,'21',a.amt)) as amt02,
			sum(decode(a.tcls,'13',a.amt)) as amt03,
			sum(a.amt) as amt,
			sum(nvl(sum(decode(a.tcls,'11',a.amt,'31',a.amt)),0)) over() as tamt01,
			sum(nvl(sum(decode(a.tcls,'21',a.amt)),0)) over() as tamt02,
			sum(nvl(sum(decode(a.tcls,'13',a.amt)),0)) over() as tamt03,
			sum(sum(a.amt)) over() as tamt,
			decode(:rdo_ord, '0', to_char(decode(d.cls,0,a.adat,null),'yyyy/mm/dd'), a.mcd) as ordkey1,
			decode(:rdo_ord, '0', a.mcd, to_char(decode(d.cls,0,a.adat,null),'yyyy/mm/dd')) as ordkey2
		from (select nvl(:chk_date, '0') as cls from dual) d,axm99 z,afm04 b,aur32 a
		where a.ktktcd=:ktktcd
			and a.dcls='3'
			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 (:ijan is null or a.ijan = :ijan)
			and (:chgcd is null or a.slcd = :chgcd)
			and (:spktcd is null or a.spktcd = :spktcd)
			and a.mcd=b.mcd(+)
			and a.ktktcd=z.ktktcd(+)
			group by to_char(decode(d.cls,0,a.adat,null),'yyyy/mm/dd'),a.mcd,b.mnm
	) z
) x
order by x.rnum
