table A                    
名称   数量
a       3
a       4
b       9
b       4
c       5table B
名称    数量
a       7
b       6
b       9
d       4
e       2如何计算用table A数量减去table B并去掉0的结果,如下显示
名称   A数量  B数量  A-B
b       13     15    -2
c       5      0      5
d       0      4      -4
e       0      2      -2

解决方案 »

  1.   


    with tb1(名称,数量)
    as(select 'a',3 union all
    select 'a',4 union all
    select 'b',9 union all
    select 'b',4 union all
    select 'c',5),
    tb2(名称,数量)
    as(
    select 'a',7 union all
    select 'b',6 union all
    select 'b',9 union all
    select 'd',4 union all
    select 'e',2),
    source1 as(
    select 名称 name,sum(数量) [count] from tb1 group by 名称
    ),
    source2 as(
    select 名称 name,sum(数量) [count] from tb2 group by 名称
    ),
    source as(
    select a.name 名称,a.count  A数量,b.count B数量,a.count-b.count AB  from source1 a,source2 b where a.name=b.name
    union all
    select b.name 名称,0  A数量,b.count B数量,0-b.count AB from source2 b where (select count(1) from source1 a where a.name=b.name)<=0
    union all
    select a.name 名称,a.count  A数量,0 B数量,a.count AB from source1 a where (select count(1) from source2 b where a.name=b.name)<=0
    )
    select * from source where [AB]!=0 order by 名称
      

  2.   


    --创建测试数据
    if OBJECT_ID('tb1_A') is not null drop table tb1_A
    create table tb1_A(名称 nvarchar(10),数量 int)
    go
    insert into tb1_A
    select 'a',3 union all
    select 'a',4 union all
    select 'b',9 union all
    select 'b',4 union all
    select 'c',5if OBJECT_ID('tb1_B') is not null drop table tb1_B
    create table tb1_B(名称 nvarchar(10),数量 int)
    go
    insert into tb1_B
    select 'a',7 union all
    select 'b',6 union all
    select 'b',9 union all
    select 'd',4 union all
    select 'e',2
    go--查询
    with T as(
    select 名称,sum(数量) 数量,'A' [Type] from tb1_A group by 名称
    union all
    select 名称,sum(数量) 数量,'B' [Type] from tb1_B group by 名称
    )
    select 名称,isnull(A,0) A数量,isnull(B,0) B数量,isnull(A,0)-isnull(B,0) [A-B] 
    from T pivot(sum(数量) for [Type] in(A,B))p 
    where isnull(A,0)<>isnull(B,0)/**结果
    名称         A数量         B数量         A-B
    ---------- ----------- ----------- -----------
    b          13          15          -2
    c          5           0           5
    d          0           4           -4
    e          0           2           -2(4 行受影响)
    **/
      

  3.   

    USE test
    go
    -->生成表tableA
    --if object_id('tableA') is not null 
    -- drop table tableA
    --Go
    Create table tableA([名称] nvarchar(1),[数量] smallint)
    Insert into tableA
    Select N'a',3
    Union all Select N'a',4
    Union all Select N'b',9
    Union all Select N'b',4
    Union all Select N'c',5
    -->生成表tableB
    --if object_id('tableB') is not null 
    -- drop table tableB
    --Go
    Create table tableB([名称] nvarchar(1),[数量] smallint)
    Insert into tableB
    Select N'a',7
    Union all Select N'b',6
    Union all Select N'b',9
    Union all Select N'd',4
    Union all Select N'e',2SELECT 
    COALESCE(a.名称,b.名称) AS 名称
    ,ISNULL(a.数量,0) AS A数量
    ,ISNULL(b.数量,0) AS B数量
    ,ISNULL(a.数量,0)-ISNULL(b.数量,0) AS [A-B] 
    FROM (
    SELECT 
    名称,SUM(数量) AS 数量 
    FROM tableA 
    GROUP BY 名称
    ) AS a
    FULL JOIN (
    SELECT 
    名称,SUM(数量) AS 数量 
    FROM tableB 
    GROUP BY 名称
    ) AS b ON a.名称=b.名称
    WHERE ISNULL(a.数量,0)-ISNULL(b.数量,0)<>0
    Go
      

  4.   

    create table testa(mc nvarchar(10),qty int)
    create table testb(mc nvarchar(10),qty int)
    insert testa select 'a',3
    insert testa select 'a',4
    insert testa select 'b',9
    insert testa select 'b',4
    insert testa select 'c',5insert testb select 'a',7
    insert testb select 'b',6
    insert testb select 'b',9
    insert testb select 'd',4
    insert testb select 'e',2select isnull(b.mc,a.mc) 名称,isnull(a.qty,0) A数量,isnull(b.qty,0) B数量,isnull(a.qty,0)-isnull(b.qty,0) 'A-B' from (select mc,sum(qty) qty from testb
    group by mc) b full join (select mc,sum(qty) qty from testa
    group by mc) a on a.mc=b.mc where isnull(a.qty,0)-isnull(b.qty,0)<>0
      

  5.   

    一、两个派生表,b表的派生表sum取负值
    二、union之后去掉数量=0的记录
    select 名称,sum(数量)
    from
    (
    select 名称,sum(数量)from a group by 名称
    union all
    select 名称,-sum(数量)from b group by 名称
    ) t
    where 数量>0
    group by 名称