SQL Server查询:调整货币列

SQL Server查询:调整货币列,sql,sql-server,tsql,sql-server-2012,Sql,Sql Server,Tsql,Sql Server 2012,我尝试在没有光标的情况下执行以下操作。可能吗 我有一个变量: DECLARE @Amount MONEY = 800; 我当前拥有的示例表: EntryID Amount -------------------------------------------- 1 200 2 250 3 600 4 100 ... ....(could be many more rows) 我希望能够更新该表,以便只显示需要添加到@Amount变

我尝试在没有光标的情况下执行以下操作。可能吗

我有一个变量:

DECLARE @Amount MONEY = 800;
我当前拥有的示例表:

EntryID    Amount
--------------------------------------------
1       200
2       250
3       600
4       100
...     ....(could be many more rows)
我希望能够更新该表,以便只显示需要添加到@Amount变量($800)中的“金额”。所以我希望这张桌子的结尾是这样的:

EntryID    Amount
--------------------------------------------
1       200 (uses up full 200)
2       250 (uses up full 250)
3       350 (uses up 350 of the 600)
还是像这样

EntryID    Amount     Distrib
--------------------------------------------
1       200        200
2       250        250
3       600        350
4       100        0
...     ...            ...
因此,
SUM()
等于
@Amount

提前谢谢

PS-我在SQL Server 2012中这样做

更新:

这是我的最终解决方案。再次感谢你,戈登:

DECLARE @Amount money = 800;
DECLARE @tmpPaymentDist TABLE (EntryID INT, Amount MONEY, Distrib MONEY);

INSERT INTO @tmpPaymentDist (EntryID, Amount) VALUES (1, 200);
INSERT INTO @tmpPaymentDist (EntryID, Amount) VALUES (2, 250);
INSERT INTO @tmpPaymentDist (EntryID, Amount) VALUES (3, 600);
INSERT INTO @tmpPaymentDist (EntryID, Amount) VALUES (4, 100);

with toupdate as (
    select t.*,
        (case when sum(amount) over (order by entryid) <= @amount
        then amount
        when sum(amount) over (order by entryid) < @amount + amount
        then @amount - (sum(amount) over (order by entryid) - amount)
        else 0
    end) as new_distrib
    from @tmpPaymentDist t
)

update T set distrib = new_distrib 
FROM @tmpPaymentDist T
INNER JOIN toupdate T2 ON T2.EntryID = T.EntryID
WHERE T2.new_distrib > 0

SELECT * FROM @tmpPaymentDist
申报@Amount money=800;
声明@tmpPaymentDist表(EntryID INT、Amount MONEY、Distrib MONEY);
插入@tmpPaymentDist(EntryID,Amount)值(1200);
插入@tmpPaymentDist(EntryID,Amount)值(2250);
插入@tmpPaymentDist(EntryID,Amount)值(3600);
插入@tmpPaymentDist(EntryID,Amount)值(4100);
以toupdate作为(
选择t.*,
(金额总和超过(按entryid排序)0时的情况)
从@tmpPaymentDist选择*

是的,您可以在不使用光标的情况下使用累积总和来执行此操作:

select t.*,
       (case when sum(amount) over (order by entryid) <= @amount
             then amount
             when sum(amount) over (order by entryid) < @amount + amount
             then @amount - (sum(amount) over (order by entryid) - amount)
             else 0
        end) as distrib
from table t;

成功了!我就知道会有那么疯狂的事情发生!哈哈,谢谢戈登的帮助
with toupdate as (
      select t.*,
             (case when sum(amount) over (order by entryid) <= @amount
                   then amount
                   when sum(amount) over (order by entryid) < @amount + amount
                   then @amount - (sum(amount) over (order by entryid) - amount)
                   else 0
              end) as new_distrib
      from table t
     )
update toudpate
    set distrib = new_distrib;