我使用的2012年版前行值加到当前行等上的SQL Server
我有这个表中的数据。
create table #temp_metrics
(
id int identity(1,1)
,prev_total int not null default(0)
,added int not null default(0)
,total int not null default(0)
)
insert into #temp_metrics
(
added
)
values
(
10
),(20),(5),(15),(9)
select * from #temp_metrics
我期待输出如下
前行总计应该是当前行prev_total
总应该是以前的行总计+添加
我使用以下查询
;WITH CTE AS (
SELECT
id
,Total
from
#temp_metrics
)
SELECT
prev.total as prev_total
,t.added
,prev.total +t.added as total
FROM
#temp_metrics t
LEFT JOIN CTE prev ON prev.id = t.id - 1
但看起来像我失去了一些东西。
如何获取图像格式的输出?
在此先感谢。
的[http://stackoverflow.com/questions/可能的复制860966/calculate-a-running-total-in-sql-server](http://stackoverflow.com/questions/860966/calculate-a-running-total-in-sql-server) – wdosanjos