select
	cuiseq,
	viseq,
	to_char(dat1, 'yyyy/mm/dd') as dat2,
	to_char(dat2, 'yyyy/mm/dd') as adat,
	spktcd,
	ktcls,
	spktnm,
	iseq31,
	vlin,
	ijan,
	jan,
	gdjnm,
	gdjstnd,
	gdcls9,
	gdcls9nm,
	pno9 as tpno,
	cst,
	fcst,
	sal,
	kflg,
	dcubcd,
	cuslip,
	bbno,
	ltxt1,ltxt2,ltxt3,txt1,
	iseq30,
	snum1,
	ntcst,
	case when :cutaxcls = '1' and :cutax = '1' then ntcst
	else cst end as dsp_ncst
from (
	select /* 物流ｾﾝﾀｰ以外 */
					a.cuiseq,b.viseq,a.vseq,a.dat1,a.dat2,a.spktcd,e.ktcls,e.ktktnm spktnm,
					a.iseq30 iseq31,a.vlin,a.ijan,f.jan,f.gdjnm,f.gdjstnd,
					f.gdcls9,g.scls5 gdcls9nm,a.pno9,a.cst,b.fcst,b.sal,b.kflg,
					b.dcubcd,b.cuslip,b.bbno,b.ltxt1,b.ltxt2,b.ltxt3,b.txt1,b.iseq30,h.snum1,b.ntcst
		from afm90 h,afm90 g,afm02 f,afm05 e,axm99 d,afm03 c,aur32 b,afr31 a
		where a.cls='14' and a.ceslip2='12'
			and a.ktktcd=:ktktcd
			and a.vseq=:vseq
			and a.cucd=:cucd
			and a.cuscd=:cuscd
			and a.cuiseq=:cuiseq
			and a.ktktcd=d.ktktcd
			and nvl(a.pno9,0)<>0
			and a.dat1>=add_months(d.sdday,-2)
			and a.spktcd=e.ktktcd and e.ktcls<>'02'
			and a.ijan=f.ijan(+)
			and g.cls(+)='6' and f.gdcls9=g.scls1(+)
			and h.cls(+)='C' and f.gdcls6=h.scls1(+)
			and a.nn1=b.iseq30(+)
			and a.cucd=c.cucd
			and ((nvl(c.cuhcls,'0')='0' and ((:sts='3') or (:sts<>'3' and b.sbat2>=d.sbat2))
						and (b.sbat>=:sbat or b.sbat=-999) and b.iseq30 is not null) or (nvl(c.cuhcls,'0')='1'))
	union all
	select  /* 物流ｾﾝﾀｰ */
					null cuiseq,b.viseq,b.vseq,b.dat2 dat1,b.adat dat2,b.spktcd,e.ktcls,e.ktktnm spktnm,
					b.iseq30 iseq31,b.vlin,b.ijan,f.jan,f.gdjnm,f.gdjstnd,
					f.gdcls9,g.scls5 gdcls9nm,abs(nvl(b.tpno,0)) pno9,b.icst cst,b.fcst,b.sal,b.kflg,
					b.dcubcd,b.cuslip,b.bbno,b.ltxt1,b.ltxt2,b.ltxt3,b.txt1,b.iseq30,h.snum1,b.ntcst
		from afm90 h,afm90 g,afm02 f,afm05 e,axm99 d,afm03 c,aur32 b
		where b.dcls='1' and b.tcls='02' and b.ent in ('1','3')
			and b.sts=0
			and b.ktktcd=:ktktcd
			and b.vseq=:vseq
			and b.cucd=:cucd
			and b.cuscd=:cuscd
			and b.viseq=:viseq
			and b.ktktcd=d.ktktcd
			and abs(nvl(b.tpno,0))<>0
			and b.dat2>=add_months(d.sdday,-2)
			and b.spktcd=e.ktktcd and e.ktcls='02'
			and b.ijan=f.ijan(+)
			and g.cls(+)='6' and f.gdcls9=g.scls1(+)
			and h.cls(+)='C' and f.gdcls6=h.scls1(+)
			and b.cucd=c.cucd
			and ((nvl(c.cuhcls,'0')='0' and ((:sts='3') or (:sts<>'3' and b.sbat2>=d.sbat2))
						and (b.sbat>=:sbat or b.sbat=-999) and b.iseq30 is not null) or (nvl(c.cuhcls,'0')='1'))
	)
order by vlin
