select a.spid,a.pici,sum(shl) shl into #1 from (select a.djbh,a.spid,a.pici,-a.shl as shl from pf_ckmx a,pf_ckhz b where a.djbh=b.djbh and a.djbh like ‘xs[a,c]%‘ and a.rq>=‘2016-04-20‘ and b.dwbh not in (select dwbh from mchk where dwmch=‘山西振东医药物流有限公司‘ and isxs=‘是‘) union all select a.djbh,a.spid,a.pici,a.shl from jh_rkmx a,jh_rkhz b where a.djbh=b.djbh and a.rq>=‘2016-04-20‘ and a.djbh like ‘jh[a,c]%‘) a group by a.spid,a.pici select spid,pici,sum(shl) shl into #2 from (select spid,批次 pici,数量 shl from fr_spkfk160421 union all select spid,pici,shl from #1) a group by spid,pici select a.*,b.chbdj*a.shl chbje,c.bmgs into #3 from #2 a join spkfjc b on a.spid=b.spid left join (select distinct bmgs,pici from sphwph where bmgs<>‘‘) c on a.pici=c.pici select b.bmgs,f.ywlxr,a.spbh,a.spmch,a.shpgg,a.dw,a.jlgg,a.shengccj,f.pihao,f.baozhiqi,f.sxrq,c.rq as 采购日期,c.rkshl,c.rkshl-b.shl as 销量,b.shl,b.chbje,d.danwbh,d.dwmch,ltrim(rtrim(b.pici)) as pici from #3 b join spkfk a on a.spid=b.spid join splsk c on c.djbh=left(b.pici,14) and c.dj_sn=replace(b.pici,left(b.pici,15),‘‘) join mchk d on c.dwbh=d.dwbh left join (select distinct pici,ywlxr,pihao,sxrq,baozhiqi from sphwph where ywlxr<>‘‘) f on b.pici=f.pici where b.shl>0
时间: 2024-11-07 21:40:53