select
  a.ijan, a.jan, a.vjan, a.gdjnm||a.gdjstnd gdnmtxt,
  a.gdjnm, a.gdjstnd, gdnm, gdstnd,
  a.gdacst, a.gdsal, a.gdbcst, a.gdcst,
  a.gdcls9, m906.scls5 gdcls9nm,
  max(b2.mcd) mcd, max(m04.mnm) mnm, 
  a.gdstp stp, decode(a.gdstp,null,'inputReadOnly','inputReadOnlyStp') readcolor
from  afm90 m906/*改廃区分*/, afm04 m04, afm02e b2, afm02e b, afm02 a
where /*b.mcd = m04.mcd(+) 仕入先*/
      a.ijan=b.ijan(+) and b2.rkbn(+)='0' and a.ijan=b2.ijan(+) and b2.mcd=m04.mcd(+) 
  and m906.cls(+)='6' and a.gdcls9=m906.scls1(+)
  and (:jan is null or a.jan like '%'||:jan)
  and (:vjan is null or a.vjan like :vjan||'%')
  and (:gdjnm is null or (upper(comnpack.MultiTOSingleBytesEx(a.gdjnm||a.gdjstnd))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:gdjnm))||'%' or
                         upper(comnpack.MultiTOSingleBytesEx(a.gdnm||a.gdstnd))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:gdjnm))||'%' or
                         upper(comnpack.MultiTOSingleBytesEx(a.gdnms))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:gdjnm))||'%') )
  and (:mcd is null or b.mcd like :mcd||'%')
  and (:mnm is null or (upper(comnpack.MultiTOSingleBytesEx(m04.mnm||m04.mnms))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:mnm))||'%' or
                         upper(comnpack.MultiTOSingleBytesEx(m04.mknm||m04.mknms))
                                like '%'||upper(comnpack.MultiTOSingleBytesEx(:mnm))||'%') )
  and (:gdcls9 is null or a.gdcls9 = :gdcls9)
  and ((NVL(:stpflg, '0') = '1' ) or (NVL(:stpflg, '0') = '0' and a.gdstp is null))
group by a.ijan, a.vjan, a.jan, a.gdjnm, a.gdjstnd, a.gdnm, a.gdstnd,
         a.gdstp, a.gdcls9, m906.scls5, a.gdacst, a.gdsal, a.gdbcst, a.gdcst