T-SQL-更新 T1 余额 + 来自 T2 的 Amt - T2 中的多行匹配 T1 - Amt 上有大小写逻辑



这可能很简单,我错过了它 - 我很想使用光标 - 但它似乎太简单了,现在必须走这条路 - 希望这里有人可以提供帮助。

这是我的代码和示例数据:

drop table if exists Customer
create table Customer (
ID int not null,
[Name] nvarchar(20),
Balance money not null
)
drop table if exists Transact
create table Transact (
ID int not null,
CustomerID int not null,
[Type] int not null,  -- 1 = payment, 2 = invoice
Amount money not null
)
insert into Customer
Values (1, 'Bob', 100), (2, 'John', 500)
insert into Transact
Values (1, 1, 2, 50), (2, 1, 1, 25), (3, 1, 2, 10),
(4, 2, 2, 100), (5, 2, 1, 50), (6, 2, 1, 200)
select * from Customer
select * from Transact

这给出了以下结果:

ID  Name    Balance
1   Bob     100.00
2   John    500.00

ID  CustomerID  Type    Amount
1   1           2       50.00
2   1           1       25.00
3   1           2       10.00
4   2           2       100.00
5   2           1       50.00
6   2           1       200.00

我要做的是将"交易"表中的"金额"值添加到"客户"表的"余额"列中。这是我尝试过的代码:

update Customer
set Customer.Balance = Customer.Balance + t.Amount
from Transact t
where Customer.ID = t.CustomerID

但这给出了这个结果 - 不是我想要的:

客户表:

ID  Name    Balance
1   Bob     150.00
2   John    600.00

我正在尝试得到这个结果:

ID  Name    Balance
1   Bob     135.00
2   John    350.00

我假设在更新中客户余额是固定的,即使它遍历了所有匹配的 Transact 记录 - 基本上最后一个获胜。

提前感谢!

PS - 要在此处添加更多信息,此游标方法可以满足您的需求,但我正在尝试使用 Update 而不是游标找到一种基于集的方法。但是要显示所需的逻辑和结果:

Declare @CustomerID int
Declare @TransType int
Declare @Amount money
declare trans_cursor cursor for
select  t.CustomerID, t.[Type], T.Amount
from    Transact t
open trans_cursor
fetch next from trans_cursor into @CustomerID, @TransType, @Amount
while @@FETCH_STATUS = 0
begin
Update C
SET C.Balance = C.Balance +
(case when @TransType = 2 then @Amount
when @TransType = 1 then (-(@Amount))
end)
from Customer C
where C.ID = @CustomerID
fetch next from trans_cursor into @CustomerID, @TransType, @Amount
end
close trans_cursor
deallocate trans_cursor

这给出了预期的结果:

ID  Name    Balance
1   Bob     135.00
2   John    350.00

再次感谢!

UPDATE c
SET c.Balance = c.Balance + t.Amount
FROM Customer AS c
CROSS APPLY (
SELECT SUM(CASE WHEN Type = 2 THEN Amount ELSE Amount * -1 END) AS Amount FROM Transact
WHERE CustomerID = c.ID
GROUP BY CustomerID 
) t

最新更新