select
	a.viseq,
	a.vseq,
	count(distinct a.ijan) as cnt,
	sum(a.amt*-1) as amt,
	to_char(a.adat,'yyyy/mm/dd') as dat,
	a.mcd,
	b.mnm,
	a.sbat2,
	sum(sum(a.amt*-1)) over() as t_amt,
  a.spktcd, m05.ktktnm as spktnm
from afm05 m05, afm04 b,aur32 a
where a.ktktcd=:ktktcd
	and a.dcls='3' and a.tcls='21'
	and a.mcd=b.mcd(+)
  and a.spktcd=m05.ktktcd(+)
	and (:mcd is null or a.mcd = :mcd)
	and (:dat1s is null or a.adat>=to_date(:dat1s,'yyyy/mm/dd'))
	and (:dat1e is null or a.adat<=to_date(:dat1e,'yyyy/mm/dd'))
	and (:spktcd is null or a.spktcd = :spktcd)
	and (:vseq is null or a.vseq = :vseq)
group by a.vseq,a.adat,a.viseq,a.sbat2,a.mcd,b.mnm,a.spktcd,m05.ktktnm
order by A.vseq,A.adat
