select
	a.viseq as viseq,
	a.vseq,
	a.hvseq,
	a.mcd,
	a.mnm,
	to_char(a.dat1, 'yyyy/mm/dd') as dat1,
	a.tcls,
	a.tclsnm,
	a.spktcd,
	a.spktnm,
	a.amt,
	a.kflg as kflg,
	sum(amt) over ( partition by ktktcd ) as t_amt,
	count(*) over ( partition by ktktcd ) as t_vseq
from af231v01 a
where a.ktktcd=:ktktcd
	and (:mcd is null or a.mcd = :mcd)
	and (:spktcd is null or a.spktcd = :spktcd)
	and (:dat1s is null or a.dat1>=to_date(:dat1s, 'yyyy/mm/dd'))
	and (:dat1e is null or a.dat1<=to_date(:dat1e, 'yyyy/mm/dd'))
	and (:vseq is null or a.vseq = :vseq)
	and (	(nvl(:pmode2, '0') = '0' and a.tcls = '11')
		or (nvl(:pmode2, '0') = '1' and a.tcls in ('11','31') and a.hvseq is null)
		or (nvl(:pmode2, '0') = '2' and a.tcls = '13')
	)
order by a.vseq, a.mcd, a.spktcd, a.tcls
