select 产品名称 , sum(费用A) 费用A, sum(费用B) 费用B,sum(费用C) 费用C,sum(费用D) 费用D
from
(
select * from tb_Income
union all
select 产品名称 , -费用A ,- 费用B,- 费用C , -[费用D] from tb_InExpend
) t
group by 产品名称

解决方案 »

  1.   

    --查询
    select 产品名称 , sum(费用A) 费用A, sum(费用B) 费用B,sum(费用C) 费用C,sum(费用D) 费用D
    from
    (
    select * from tb_Income
    union all
    select 产品名称 , -费用A ,- 费用B,- 费用C , -[费用D] from tb_InExpend
    ) t
    group by 产品名称--插入临时表
    select 产品名称 , sum(费用A) 费用A, sum(费用B) 费用B,sum(费用C) 费用C,sum(费用D) 费用D
    into 临时表
    from
    (
    select * from tb_Income
    union all
    select 产品名称 , -费用A ,- 费用B,- 费用C , -[费用D] from tb_InExpend
    ) t
    group by 产品名称
      

  2.   


    select a.产品名称,a.费用A-b.费用A 费用A,a.费用b-b.费用b 费用b,a.费用c-b.费用c 费用c,a.费用d-b.费用d 费用d into #temp from tb_Income a,tb_InExpend d where a.产品名称=b.产品名称
      

  3.   

    select A.产品名称,A.费用A-B.费用A,A.费用B-B.费用B,A.费用C-B.费用C,A.费用D-B.费用D
    into #temp
    from tb_Income A,tb_InExpend B
    where A.产品名称=B.产品名称
      

  4.   

    select a.产品名称,a.费用A-b.费用A 费用A,a.费用b-b.费用b 费用b,a.费用c-b.费用c 费用c,a.费用d-b.费用d 费用d into #temp from tb_Income a,tb_InExpend b where a.产品名称=b.产品名称
      

  5.   

    isnert into #r
    select 
      a.产品名称,
      sum(a.费用A-b.费用A) as 费用A,
      sum(a.费用B-b.费用B) as 费用B,
      sum(a.费用C-b.费用C) as 费用C,
      sum(a.费用D-b.费用D) as 费用D
    from tb_Income a
    left join tb_InExpend b
    on a.产品名称=b.产品名称
    group by a.产品名称
      

  6.   

    非常感谢,各位的回答均能实现功能
    请教一下:into #temp是不是和 into 临时表 没有区别
      

  7.   

    into #temp --你用的是临时表
    into 临时表 --我没有加#,是正式表,即所谓的临时使用的表,用完后删除.