select id , name,spec,amount sum(costs)
from 
select id , name,spec,amount,sum(costs) costs
   from table_A
   where 、、、
   group by id , name,spec,amount
union all
select id , name,spec,amount,sum(costs) costs
   from table_B
   where 、、、
   group by id , name,spec,amount)group by id , name,spec,amount

解决方案 »

  1.   

    select id , name,spec,amount sum(costs)
    from 
    (select id , name,spec,amount,costs costs
       from table_A
    union all
    select id , name,spec,amount,costs costs
       from table_B)
    group by id , name,spec,amount这样执行的性能能比每个select都加group好些吧
      

  2.   

    table_A 和table_B costs字段的数据结构不一样?怎么办?
    我用你的方法试了,提示  无效的列名还有其它方法嘛?
    请指教
      

  3.   

    应该是这样的2个表
       select id , name,spec,amount,sum(a.costs)
       from table_A a
       where 、、、
       group by id , name,spec,amount   select id , name,spec,amount,sum(b.charges)
       from table_B b
       where 、、、
       group by id , name,spec,amounta.costs 和 b.charges 的数据类型是一样的我现在需要得到  按照 id , name,spec,amount 分组的 sum(a.costs+b.charges)该如何写?谢谢
      

  4.   

    答案:
    group by 时起个别名COST就行了:select id , name,spec,amount sum(COST)
    from 
    (
      select id , name,spec,amount,sum(a.costs) COST
       from table_A a
       where 、、、
       group by id , name,spec,amountunion all   select id , name,spec,amount,sum(b.charges) COST
       from table_B b
       where 、、、
       group by id , name,spec,amount)
    group by id , name,spec,amount