I am using version 2012.
I have this table with the data.
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
I am expecting the output as below
Previous row total should be current row prev_total
Total should be the previous row total+added
I am using the below query
;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
But looks like I am missing something.
How can I get the output as in the image format ?
Thanks in advance.