select k.id,sum(k.qty) from (select * from t_stock_in union all select * from t_stock_out) k group by k.id

解决方案 »

  1.   

    select a.id ,a.qty + b.qty as qty
    from t_stock_in left join t_stock_out
    on a.id = b.id
      

  2.   

    感谢各位,再问如果有三个表以上两个不变,再增加一个表t_stock
    依然有两个字段,id 和 qty
    用t_stock 中的每一条数据,加上t_stock_in 中的数据,再减去t_stock_out
    中的每一条数据,如何得到结果呢???
      

  3.   

    select *,
    qty + (select sum(qty) from t_stock_in where id = t_stock.id) - (select sum(qty) from t_stock_Out where id = t_stock.id)
    from t_stock
      

  4.   

    select *,qty 
        + (select sum(qty) 
             from t_stock_in 
            where id = t_stock.id) 
        - (select sum(qty) 
             from t_stock_Out 
            where id = t_stock.id)
    from t_stock
      

  5.   

    playyuer(小干部儿) :我是想得到计算后a:??b:??c:??d:?阿,你这样得不到阿
      

  6.   

    建立临时表,再把全部表的记录insert 临时表,再 group by id
      

  7.   

    或者select [id]+':'+convert(varchar(10),qty)
    from 
    (
    select [id],qty=sum(qty)
    from 
    (
    select a.* 
    from a
    union 
    select b.* 
    from b
    )as t
    group by [id]
    )as tt
      

  8.   

    select k.id,':',sum(k.qty) 
      from (select * from t_stock_in  
             union all 
            select * from t_stock_out) k
     group by k.id