[英]Add per row the total value in SQL
您可以使用累計總和:
select [Order], Game, Points,
sum(Points) over (partition by Game order by [Order]) as CumePoints
from t;
您應該避免為表或列名稱使用保留的單詞和關鍵字。 換句話說, Order
是列名的壞名字,因為它需要被轉義。
如果您在T-SQL中執行此操作,這就是我會執行的操作
if object_ID('tempdb..#Temp') IS NOT NULL drop table #Temp
create table #Temp (id int, Game nvarchar(100), Points int)
insert into #Temp (id, Game, Points)
values
(1, 'A', 1),
(2, 'A', 2),
(3, 'B', 5),
(4, 'B', 5),
(5, 'C', 4),
(6, 'C', 8)
select id,
Game,
Points,
SUM(Points) over (partition by Game order by id)
from #Temp
一個舊的解決方案是使用這樣的查詢:
SELECT
t1.id, t1.Game, t1.Points, SUM(t2.Points) [Points (Add)]
FROM
yourTable t1 JOIN
yourTable t2 ON t1.Game = t2.Game AND t1.id >= t2.id
GROUP BY
t1.id, t1.Game, t1.Points
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.