title: Previous row value added to the current row and so on SQL Server
body: <p>I am using version 2012.</p>
  
  <p>I have this table with the data.</p>
  
  <pre><code>        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 
  </code></pre>
  
  <p>I am expecting the output as below</p>
  
  <p><a href="https://i.stack.imgur.com/gJlsC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/gJlsC.png" alt="enter image description here"></a></p>
  
  <p>Previous row total should be current row prev_total</p>
  
  <p>Total should be the previous row total+added</p>
  
  <p>I am using the below query</p>
  
  <pre><code>      ;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
  </code></pre>
  
  <p>But looks like I am missing something.</p>
  
  <p>How can I get the output as in the image format ?</p>
  
  <p>Thanks in advance.</p>