0
我沒有看到我做錯了什麼。我在經常賬戶1000
,從這個數額我想支付佣金一個接一個。最後一筆佣金必須保持不足以支付(20 + 1 + 1000) - 1000 = 2
的金額。從表中減去金額
declare @CommissionTable table(AccountId INT, Amount DECIMAL(18,2))
INSERT INTO @CommissionTable VALUES(17584981,20)
INSERT INTO @CommissionTable VALUES(17584982,1)
INSERT INTO @CommissionTable VALUES(17584983,1000)
DECLARE @CurrentAmount DECIMAL(18,2)
SET @CurrentAmount = 1000
declare @ComissToPay decimal(18,2)
;WITH [comis] AS (SELECT Amount FROM @CommissionTable)
UPDATE [comis] set @ComissToPay = CASE
WHEN @CurrentAmount < CM.Amount THEN @CurrentAmount
WHEN @CurrentAmount > CM.Amount THEN CM.Amount
END,
Amount = CM.Amount - isnull(@ComissToPay,0),
@CurrentAmount = @CurrentAmount - isnull(@ComissToPay,0)
from [comis] CM
SELECT * FROM @CommissionTable
(20 + 1 + 1000) - 1000 = 2你怎麼得到2? –
偶然得到21分嗎? – thepirat000
我想在這裏幫助你,但我不確定你要找的最終結果是什麼樣子。你能發表你想看到的嗎? –