问题是这样的:
有两张表A,B;
表A有两个字段type(主键)表示商品品种, num字段表示商品总数量。
表B也有两个字段type表示商品品种,num已经售出商品数量。
表A的数据如下:
type  num
A     1000
B     2000
C     2500表B的数据如下:
type  num
A     100
B     200
A     300
A     100
B     200现在要查询A表中剩余商品的数量,即查询结果为:
type   num
A      500
B      1600
C      2500不知道这样的sql语句怎么写,请大家帮帮忙,谢谢!

解决方案 »

  1.   

    select a.type,a.num - isnull(b.num,0) as num
    from ta a
    left join (select type,sum(num) as num from tb group by type) b
    on a.type = b.type
      

  2.   

    select type,num-(select sum(num) from B where type=A.type) as num from A
      

  3.   

    select A.type,sum(A.num-B.num1) from A join
    (
      select type sum(num) num1 from B group by type
    ) B on A.type=B.type group by A.type 
      

  4.   

    select 
      a.[type],
      a.num-isnull(b.num,0)
    from 
      a
    left join
      (select [type],sum(num) as num from b group by [type]) b
    on
      a.[type]=b.[type]
      

  5.   

    select type,num-(select sum(num) from tbB where type=a.type) as num from tbA a
      

  6.   

    if object_id('a') is not null
      drop table a
    go
    create table a([type] varchar(10),[num] int)
    insert a select 'A',1000
    insert a select 'B',2000
    insert a select 'C',2500
    go
    if object_id('b') is not null
      drop table b
    go
    create table b([type] varchar(10),[num] int)
    insert b select 'A','100'
    insert b select 'B','200'
    insert b select 'A','300'
    insert b select 'A','100'
    insert b select 'B','200'
    goselect type,num-isnull((select sum(num) from b where type=t.type),0) num from a t/*
    type       num
    ---------- -----------
    A          500
    B          1600
    C          2500(3 行受影响)
    */
      

  7.   

    select ta.type,ta.num - isnull(tb.num,0) as num
    from A ta
    left join (select type,sum(num) as num from B group by type) tb
    on ta.type = tb.type
      

  8.   

    select a.type,isnull(a.num-b.num,a.num) num from a left join (select type,sum(num) as num from b group by type) as b on b.type=a.type
      

  9.   

    --定义临时表ta,并插入数据
    declare @ta table(type  varchar(2),num smallint)
    insert into @ta
    select  'A',1000
    union all
    select 'B',2000
    union all
    select 'C',2500--定义临时表tb,并插入数据
    declare @tb table(type varchar(2),num smallint)
    insert into @tb 
    select 'A',100
    union all
    select 'B',200
    union all
    select 'A',300
    union all
    select 'A',100
    union all
    select 'B',200--查询出每种商品的剩余数量
    select t1.type,case when t2.soldqty is null then num else num-t2.soldqty end as leftQty from  @ta  t1 left join 
    (
    select type,sum(num) as soldQty from @tb group by type
    ) t2  on t1.type=t2.type
    查询结果:
    type leftQty     
    ---- ----------- 
    A    500
    B    1600
    C    2500(所影响的行数为 3 行)
      

  10.   


    SELECT T1.TYPE,T1.NUM
    FROM
    (
    SELECT T2.TYPE,(T2.NUM - T3.NUM) NUM FROM
    A T2,
    (SELECT SUM(NUM) FROM B GROUP BY TYPE) T3 WHERE T2.TYPE = T3.TYPE
    ) T1
      

  11.   

    不好意思,上面写错了,下面运行正确:SELECT T1.TYPE,T1.NUM
    FROM
    (
    SELECT A.TYPE TYPE,(A.NUM - T3.NUM) NUM FROM
    A,
    (SELECT SUM(B.NUM) NUM,B.TYPE TYPE FROM B GROUP BY TYPE) T3 WHERE A.TYPE = T3.TYPE
    ) T1
    UNION
    SELECT TYPE,NUM FROM A
    WHERE TYPE NOT IN
    (
    SELECT TYPE FROM 
    (
    SELECT T1.TYPE,T1.NUM
    FROM
    (
    SELECT A.TYPE TYPE,(A.NUM - T3.NUM) NUM FROM
    A,
    (SELECT SUM(B.NUM) NUM,B.TYPE TYPE FROM B GROUP BY TYPE) T3 WHERE A.TYPE = T3.TYPE
    ) T1
    )T5
    )
      

  12.   

    select type,(num-(select sum(num) from b where b.type=a.type)) as result from a
      

  13.   

    --定义临时表ta,并插入数据
    declare @ta table(type  varchar(2),num smallint)
    insert into @ta
    select  'A',1000
    union all
    select 'B',2000
    union all
    select 'C',2500--定义临时表tb,并插入数据
    declare @tb table(type varchar(2),num smallint)
    insert into @tb 
    select 'A',100
    union all
    select 'B',200
    union all
    select 'A',300
    union all
    select 'A',100
    union all
    select 'B',200--查询出每种商品的剩余数量
    select t1.type,case when t2.soldqty is null then num else num-t2.soldqty end as leftQty from  @ta  t1 left join 
    (
    select type,sum(num) as soldQty from @tb group by type
    ) t2  on t1.type=t2.type
      

  14.   


    select A.type as type1,(case when C.summ is null then A.num else (A.num-C.summ) end) as csum from #ta A left join(select type,sum(num)as summ from #tb group by type) C on  A.type=C.type  温州it程序员群32677495,温州的IT人++,大家多多交流
      

  15.   


    SELECT A.[type]
          ,m=(A.[num]-isnull(N.s,0))
      FROM A left join 
    (
    SELECT [type]
          ,sum([num]) as s
      FROM [B] group by [type]
    ) N 
    on A.[type]=N.[type]