select a.pluno,a.pluname,a.spec,a.qty,d.qty,e.qty,a.dptno,substring(a.dptno,1,3),f.dptname,substring(a.dptno,1,4),g.dptname,substring(a.dptno,1,5),h.dptname
from lhxs201402 a
left join lhspm b on a.pluno=b.pluid
left join xbspm c on b.barcode=c.bcd
left join (select pluno,sum(qty) qty from xbxs where rq between ‘20140601‘ and ‘20140914‘ group by pluno ) d on c.pluno=d.pluno
left join (select pluno,sum(qty) qty from dbxs where rq between ‘20140601‘ and ‘20140914‘ group by pluno ) e on c.pluno=e.pluno
left join lhflm f on substring(a.dptno,1,3)=f.dptno
left join lhflm g on substring(a.dptno,1,4)=g.dptno
left join lhflm h on substring(a.dptno,1,5)=h.dptno
原文地址:http://www.cnblogs.com/nlg3413/p/3971036.html