select aa.id,sum(aa.sl) as sl 
from (
select a.id as id,a.sl from a表  where 条件
union all
select b.id ,b.sl from b表  where 条件
union all
select c.id,-c.sl from c表  where 条件
union all
select d.id,-d.sl from d表  where 条件 ) aa
group by aa.id

解决方案 »

  1.   

    select id,sl=sum(sl)
    from
    (
    select a.id,a.sl from a,a从表 where a.id=a从表.did and a从表.rq=...
    union all
    select b.id,b.sl from b,b从表 where a.id=b从表.did and b从表.rq=...
    union all
    select c.id,-c.sl from c,c从表 where a.id=c从表.did and c从表.rq=...
    union all
    select d.id,-d.sl from d,d从表 where a.id=d从表.did and d从表.rq=...
    ) t
    group by id
      

  2.   

    问题是这样的四个表
    Table_llsqmx //领料单
    Table_rkdmx//入库
    Table_clqsfp//缺少发票
    Table_clbsmx//报损
    需求出数据入库+缺少发票-领料单-报损
      

  3.   

    Table_Rkd//入库单主表
    Table_rkdmx//入库从表主表中都有id,DJRQ字段先对主从表关联然后
    求出数据,按cl_id求(并且有日期限制)
    入库+缺少发票-领料单-报损
      

  4.   

    select a.cl_id,(a.sl - b.sl + c.sl - d.sl) as sl 
    from ( select e.cl_id, sum(e.sl) as sl from Table_rkdmx as e,Table_rkd as f where e.rkd_id=f.rkd_id group by e.cl_id ) a,
      ( select g.cl_id, sum(g.sl) as sl from Table_lldmx as g,Table_lld as h where g.LLD_id=h.LLD_id group by g.cl_id ) b,
      ( select cl_id, sum(sl) as sl from Table_clqsfp  group by cl_id ) c,
      ( select i.cl_id, sum(i.sl) as sl from Table_clbsmx as i,Table_clbsd as j where i.BSD_id=j.BSD_id group by i.cl_id ) d
    where a.cl_id = b.cl_id and a.cl_id = c.cl_id and a.cl_id = d.cl_idok成功了