我有一张桌子,上面有来自不同客户的许多分期付款。每个客户都有可用余额。
我想显示客户可以支付的所有分期付款。
客户 1 可用余额 400 客户 2 可用余额 500
分期付款表:
Customer ID Instalment amount Available
1 150 400
1 150 400
1 150 400
1 150 400
1 150 400
1 150 400
1 150 400
2 200 500
2 200 500
2 200 500
2 200 500
2 200 500
2 200 500
以下是我想要的结果
Customer ID Instalment amount Available
1 150 400
1 150 400
2 200 500
2 200 500
下面的代码不起作用
DECLARE @DrACAvailable decimal,
@PayAmount decimal,
@RunningTotal decimal
SET @RunningTotal = 0
DECLARE rt_cursor CURSOR
FOR
SELECT T2.PayAmount, T2.DrACAvailable
FROM LoanAutoPayTransactions T2
INNER JOIN LoanAutoPayTransactions T1 on T2.LRAC=T1.LRAC
OPEN rt_cursor
FETCH NEXT FROM rt_cursor INTO @PayAmount, @DrACAvailable
WHILE @@FETCH_STATUS = 0
BEGIN
SET @RunningTotal = @RunningTotal + @PayAmount
IF @RunningTotal >= @DrACAvailable BREAK
UPDATE LoanAutoPayTransactions SET PayAmount=@RunningTotal WHERE StartDate=(SELECT MIN(T2.StartDate) FROM LoanAutoPayTransactions T2 WHERE T2.LRAC=LRAC)
FETCH NEXT FROM rt_cursor INTO @PayAmount, @DrACAvailable
END
CLOSE rt_cursor
DEALLOCATE rt_cursor