更新时间:2022-11-27 23:18:31
试试这个:
Try this:
select id, amount,
(select sum(amount) from tablename t2 where t2.id <= t1.id) result
from tablename t1
你可以做类似这样的东西 -
You can do something like this-
SELECT T1.Id, T1.Amount, SUM(T2.Amount) AS Result
FROM YourTable T1
INNER JOIN YourTable T2 ON T1.Id >= T2.Id
GROUP BY T1.Id, T1.amount
ORDER BY T1.Id
示例:
Example:
DECLARE @temp_table AS TABLE(Id INT, Amount INT)
INSERT INTO @temp_table
SELECT 1 Id,10 Amount
UNION
SELECT 2,20
UNION
SELECT 3,30
UNION
SELECT 4,40
UNION
SELECT 5,50
SELECT T1.Id, T1.Amount, SUM(T2.Amount) AS Result
FROM @temp_table T1
INNER JOIN @temp_table T2 ON T1.Id >= T2.Id
GROUP BY T1.Id, T1.amount
ORDER BY T1.Id
希望,它有帮助:)
Hope, it helps :)