当前位置: 代码迷 >> Sql Server >> 求两表求和的SQL语句解决方法
  详细解决方案

求两表求和的SQL语句解决方法

热度:47   发布时间:2016-04-27 20:34:49.0
求两表求和的SQL语句
有两张表Table1,Table2。table1数据如下:
ID                   product                     amount
----------------------------------
1                     A                                   10
2                     A                                   12
3                     B                                   5
4                     B                                   11
5                     A                                   3


table2数据如下:
ID                   product                     amount
----------------------------------
1                     A                                   5
2                     A                                   10
3                     B                                   5


我想求出table1中A、B的总数,再减去table2中A、B的总数,然后生成新表
ID                   product                     amount
----------------------------------
1                     A                                   10
2                     A                                   11

这个SQL怎么写?

------解决方案--------------------
select a.product,(a.amount -b.amount ) from
(select product,sum(amount) amount from table1 group by product)a
(select product,sum(amount) amount from table2 group by product)b
where a.product=b.product

  相关解决方案