货物名称表内容ID(主键)   货物名称
1                货 A
2                货 B
3                货 C
4                货 D================================================已出货表出货ID(主键)   出货名称("货物名称表"的名称)              赚      亏
1                  货 A                              100      30
2                  货 D                               50       3
3                  货 A                               30      10
4                  货 A                              650      20
5                  货 C                               70       8
5                  货 D                              555      80======================================================统计表 <---问题:我要得到这样的统计表内容名       总赚           总亏
 货A        780          60  <------总赚=100+30+650,总亏=30+10+20
 货B         0           0   <------因为[出货表]的"出货名称" 里没有 "货B" ,所以赚亏都为0
 货D        605          83  <------总赚=50+555,总亏=3+80
 货C         70          8

解决方案 »

  1.   

    TO:kaikai_kk不小心打错了,主键没有重复。
      

  2.   

    恩?
    select 内容名 =  b.货物名称,
            总赚 = sum(b.赚 ),
            总亏= sum(b.赚- b.亏)
    from  已出货表 b
    group by 出货名称
      

  3.   

    --> 测试数据: #1
    if object_id('tempdb.dbo.#1') is not null drop table #1
    create table #1 (内容ID int,货物名称 varchar(3))
    insert into #1
    select 1,'货A' union all
    select 2,'货B' union all
    select 3,'货C' union all
    select 4,'货D'
    --> 测试数据: #2
    if object_id('tempdb.dbo.#2') is not null drop table #2
    create table #2 (出货ID int,出货名称 varchar(3),赚 int,亏 int)
    insert into #2
    select 1,'货A',100,30 union all
    select 2,'货D',50,3 union all
    select 3,'货A',30,10 union all
    select 4,'货A',650,20 union all
    select 5,'货C',70,8 union all
    select 5,'货D',555,80
    select 内容名=a.货物名称,总赚=sum(isnull(b.赚,0)),总亏=sum(isnull(b.亏,0)) from #1 a left join #2 b on a.货物名称=b.出货名称 group by a.货物名称/*
    内容名 总赚        总亏
    ----   ----------- -----------
    货A    780         60
    货B    0           0
    货C    70          8
    货D    605         83
    */
      

  4.   

    哦,错了select 内容名 =  a.货物名称,
            总赚 = sum(b.赚 ),
            总亏= sum(b.赚- b.亏)
    from  货物名称表 a
    left join 已出货表 b on a.货物名称 = b.货物名称
    group by a.出货名称
      

  5.   

    加个判断  [code=SQL
    select 内容名 =  a.货物名称,
            总赚 = sum(isnull(b.赚,0) ),
            总亏= sum(isnull(b.赚,0)- isnull(b.亏,0))
    from  货物名称表 a
    left join 已出货表 b on a.货物名称 = b.货物名称
    group by a.出货名称[/code]
      

  6.   

     
    select 内容名 =  a.货物名称, 
            总赚 = sum(isnull(b.赚,0) ), 
            总亏= sum(isnull(b.赚,0)- isnull(b.亏,0)) 
    from  货物名称表 a 
    left join 已出货表 b on a.货物名称 = b.货物名称 
    group by a.出货名称
      

  7.   

    晕,不要减了select 内容名 =  a.货物名称, 
            总赚 = sum(isnull(b.赚,0)), 
            总亏= sum(isnull(b.亏,0)) 
    from  货物名称表 a 
    left join 已出货表 b on a.货物名称 = b.货物名称 
    group by a.出货名称
      

  8.   

    select a.货物名称 内容名,sum(isnull(b.赚,0)) 总赚,sum(isnull(b.亏,0)) 总亏 
    from @货物名称表 a
    left join @已出货表 b 
    on a.货物名称=b.出货名称
    group by a.货物名称
      

  9.   

    select a.货物名称 内容名,sum(isnull(b.赚,0)) 总赚,sum(isnull(b.亏,0)) 总亏 
    from 货物名称表 a
    left join 已出货表 b 
    on a.货物名称=b.出货名称
    group by a.货物名称
      

  10.   

    救命啊,我还没问完.http://topic.csdn.net/u/20080406/15/1dcd0a56-2cd8-4a94-a160-9f9ea930682d.html