select x.*,
	ceil(rnum / :lcnt) as pgno,
	0 chkval,
	max(rnum) over() as maxnum
from (
	select
		ROW_NUMBER() OVER (ORDER BY z.adat) as rnum,
		z.*,
		case
			when nvl(z.amt01,0)=0 then
			 	case when nvl(z.amt02, 0) = 0 then null 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
			to_char(a.adat,'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
		from afm03 c,axm99 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.chgcd = :chgcd)
			and (:spktcd is null or a.spktcd = :spktcd)
			and a.ktktcd=b.ktktcd(+)
			and a.cucd=c.cucd(+)
		group by to_char(a.adat,'yyyy/mm/dd')
	) z
) x
order by x.rnum
