select a.ktktcd, a.sts, a.dcls, a.dcls1, a.ceslip2,
        a.spktcd, a.spktnm, a.vseq,
        to_char(a.dat1,'yyyy/mm/dd') dat1,
        a.mcd, a.mnm, a.amt,
        decode(a.sts,'N','未確定',null) stsnm,
        sum(amt) over ( partition by ktktcd ) t_amt,
        count(*) over ( partition by ktktcd ) t_vseq
from af212v02 a
where a.ktktcd=:ktktcd
  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 (:mcd is null or a.mcd = :mcd)
  and ((nvl(:pmode,'0')= '0' and dcls in ('1','2') and dcls1='1' and ceslip2 in ('01', '03') and sts='N')  /*発注入力からの検索は sts ='N'*/
    or (nvl(:pmode,'1')= '1' and dcls='1' and dcls1='4' and ceslip2='05' and nvl(sts,' ')=' ')             /*直送発注入力は nvl(sts,' ')=' '*/
    or (nvl(:pmode,'1')= '2' and dcls in ('1','2') and dcls1='1' and ceslip2 in ('01', '03') and sts=' ')) /*仕入入力は確定済みのみ sts=' '*/
order by a.vseq, a.spktcd, a.mcd