title: T-SQL using SUM for a running total
body: <p>I have a simple table with some dummy data setup like:</p>
  
  <pre><code>|id|user|value|
  ---------------
   1  John   2
   2  Ted    1
   3  John   4
   4  Ted    2
  </code></pre>
  
  <p>I can select a running total by executing the following sql(MSSQL 2008) statement:</p>
  
  <pre><code>SELECT a.id, a.user, a.value, SUM(b.value) AS total
  FROM table a INNER JOIN table b
  ON a.id &gt;= b.id
  AND a.user = b.user
  GROUP BY a.id, a.user, a.value
  ORDER BY a.id
  </code></pre>
  
  <p>This will give me results like:</p>
  
  <pre><code>|id|user|value|total|
  ---------------------
   1  John   2     2
   3  John   4     6
   2  Ted    1     1
   4  Ted    2     3
  </code></pre>
  
  <p>Now is it possible to only retrieve the most recent rows for each user? So the result would be:</p>
  
  <pre><code>|id|user|value|total|
  ---------------------
   3  John   4     6
   4  Ted    2     3
  </code></pre>
  
  <p>Am I going about this the right way? any suggestions or a new path to follow would be great!</p>