select t.ypm,t.gg,t.cd,t.zxdw,t.nbbm,m.dwmc,m.bzdj,t.sysl+t.bysl-t.bfsl as kcs,t.kcxx from
(select
(select ypm from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as ypm,
(select gg from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as gg,
(select cd from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as cd,
(select zxdw from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as zxdw,
a.nbbm,a.sysl,a.bysl,a.bfsl,a.kcxx from ZY_YP1_YKSZ a where sysl+bysl-bfsl<=kcxx) as t
left join
(select distinct nbbm as nbbm1,
(select dmmc from zy_sys2_cgdw c where c.dm=a.dw) as dwmc,
a.dw,
bzdj from zy_yp1_sfmx a where cfbj='t' and a.jzrq=(select max(jzrq) from zy_yp1_sfmx b where cfbj='t' and a.nbbm=b.nbbm )) as m
on t.nbbm=m.nbbm1
(select
(select ypm from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as ypm,
(select gg from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as gg,
(select cd from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as cd,
(select zxdw from zy_sys2_ypzdk b where b.nbbm=a.nbbm) as zxdw,
a.nbbm,a.sysl,a.bysl,a.bfsl,a.kcxx from ZY_YP1_YKSZ a where sysl+bysl-bfsl<=kcxx) as t
left join
(select distinct nbbm as nbbm1,
(select dmmc from zy_sys2_cgdw c where c.dm=a.dw) as dwmc,
a.dw,
bzdj from zy_yp1_sfmx a where cfbj='t' and a.jzrq=(select max(jzrq) from zy_yp1_sfmx b where cfbj='t' and a.nbbm=b.nbbm )) as m
on t.nbbm=m.nbbm1