这是我的数据源,需要实现@test 表的Qty要减去@test1表的Qty,条件是Customer相同,不清楚如何操作,没有思路
[SQL] 纯文本查看 复制代码
declare @test table
(
ID int identity(1,1),
Customer varchar(15),
Qty int
)
declare @test1 table
(
ID int identity(1,1),
Customer varchar(15),
Qty int
)
insert into @test values('113375',2700)
insert into @test values('113375',4500)
insert into @test values('113375',8100)
insert into @test1 values('113375',2100)
insert into @test1 values('113375',2400)
insert into @test1 values('113375',2100)
insert into @test1 values('113375',2100)
insert into @test1 values('113375',1800)
insert into @test1 values('113375',2100)
--在Customer相同时,将@test表的Qty依次减去@test1表的Qty,
--2700-2100=600
--4500+600-2400=2700
--2700-2100=600
--8100+600-2100=6600
--6600-1800=4800
--4800-2100=2700
--最终@test 输出3 111375 2700
|