三个结构一致的数据表a1、a2、a3,都包含列“物品名称,编号,数量”,如何将a1表中某些物品的数量与a2表中对应编号的物品的数量相减,相减结果放在a3表中的数量列(同时a1,a2表中相同编号的物品名称和编号也相应放入a3表,且确保编号唯一),如下:
a1                                                 
物品名称   编号    数量                    
纸张       01       100
铅笔       02        50
纸张       01        20
钢笔       03        20
a2
物品名称   编号    数量                    
铅笔       02       40
纸张       01       20
纸张       01       60运算结果放入a3表,其中纸张数量为100+20-20-60=40,铅笔数量:50-40=10
a3表
物品名称   编号    数量
纸张       01       40
铅笔       02       10

解决方案 »

  1.   

    insert a3
    select 物品名称,编号,sum(数量)数量
    from
    (select 物品名称,编号,数量 from a1
     union all
     select 物品名称,编号,-数量 from a2
    )t
    group by 物品名称,编号
      

  2.   

    select 物品名称,编号,sum(数量)from 
    (
    select 物品名称,编号,数量  from ta
    union all
    select 物品名称 ,号 -,量  from ta
    ) a
    group by  物品名称,编号
      

  3.   

    insert a3
    select a.物品名称,a.编号,a.数量-b.数量 as 数量
    from (select 物品名称,编号,sum(数量) as 数量 from a1 group by 物品名称,编号) a
    ,(select 物品名称,编号,sum(数量) as 数量 from a2 group by 物品名称,编号) b
    where a.编号=b.编号
      

  4.   

    insert a3(物品名称,编号,数量)
    select 物品名称,编号,sum(数量)
    from
    (
      select 物品名称,编号,数量 from a1
      union all
      select 物品名称,编号,-数量 from a2
    ) t
    group by 物品名称,编号
      

  5.   

    insert a3
    select 物品名称,编号,sum(数量)数量
    from
    (select 物品名称,编号,数量 from a1
     union all
     select 物品名称,编号,-数量 from a2
    )T
    group by 物品名称,编号
      

  6.   

    insert into tc
    select 物品名称,编号,sum(数量)from 
    (
    select 物品名称,编号,数量  from ta
    union all
    select 物品名称 ,编号, -数量  from ta
    ) a
    group by  物品名称,编号
      

  7.   

    create table #a1(物品名称  varchar(10),编号 int,数量 int)
    insert #a1 select '纸张', 01, 100
    insert #a1 select '铅笔',  02, 50
    insert #a1 select '纸张',  01, 20
    insert #a1 select '钢笔',  03, 20create table #a2(物品名称  varchar(10),编号 int,数量 int)
    insert #a2 select '铅笔', 02, 40
    insert #a2 select '纸张', 01, 20
    insert #a2 select '纸张', 01, 60create table #a3(物品名称  varchar(10),编号 int,数量 int)
    insert #a3
    select a.物品名称,a.编号,a.数量-b.数量 as 数量
    from (select 物品名称,编号,sum(数量) as 数量 from #a1 group by 物品名称,编号) a
    ,(select 物品名称,编号,sum(数量) as 数量 from #a2 group by 物品名称,编号) b
    where a.编号=b.编号select * from #a3物品名称                编号          数量
    ---------- ----------- -----------
    纸张                   1          40
    铅笔                   2          10(2 行受影响)
      

  8.   

    ----------------------------------------------------------------
    -- Author  :fredrickhu(小F,向高手学习)
    -- Date    :2010-03-18 13:05:11
    -- Verstion:
    --      Microsoft SQL Server  2000 - 8.00.2055 (Intel X86) 
    -- Dec 16 2008 19:46:53 
    -- Copyright (c) 1988-2003 Microsoft Corporation
    -- Personal Edition on Windows NT 5.1 (Build 2600: Service Pack 3)
    --
    ----------------------------------------------------------------
    --> 测试数据:[a1]
    if object_id('[a1]') is not null drop table [a1]
    go 
    create table [a1]([物品名称] varchar(4),[编号] varchar(2),[数量] int)
    insert [a1]
    select '纸张','01',100 union all
    select '铅笔','02',50 union all
    select '纸张','01',20 union all
    select '钢笔','03',20
    --> 测试数据:[a2]
    if object_id('[a2]') is not null drop table [a2]
    go 
    create table [a2]([物品名称] varchar(4),[编号] varchar(2),[数量] int)
    insert [a2]
    select '铅笔','02',40 union all
    select '纸张','01',20 union all
    select '纸张','01',60
    --------------开始查询--------------------------
    create table a3(物品名称  varchar(10),编号 int,数量 int)insert 
     a3
    select
     a.物品名称,a.编号,isnull(a.数量,0)-isnull(b.数量,0) as 数量
    from
     (select 物品名称,编号,sum(数量) as 数量 from a1 group by 物品名称,编号) a,
     (select 物品名称,编号,sum(数量) as 数量 from a2 group by 物品名称,编号) b
    where
     a.编号=b.编号
    and
     a.物品名称=b.物品名称
    select * from a3
    ----------------结果----------------------------
    /* 物品名称       编号          数量          
    ---------- ----------- ----------- 
    纸张         1           40
    铅笔         2           10(所影响的行数为 2 行)
    */
      

  9.   

    在查询分析器中执行以下语法时提示“没有为第3列(属于‘a’)制定列,如何解决?
    insert 
     a3
    select
     a.物品名称,a.编号,isnull(a.数量,0)-isnull(b.数量,0) as 数量
    from
     (select 物品名称,编号,sum(数量) as 数量 from a1 group by 物品名称,编号) a,
     (select 物品名称,编号,sum(数量) as 数量 from a2 group by 物品名称,编号) b
    where
     a.编号=b.编号
    and
     a.物品名称=b.物品名称
    select * from a3