title: SQL remove from running total
body: <p>I have a problem that I don't know how to fix .. here is the code and wanting result</p>
  
  <pre><code>if object_id('tempdb..#A') IS NOT NULL drop table #A
  create table #A (ID int, Value decimal(6,2), value2 decimal(6,2), Result decimal(6,2))
  
  insert into #A (ID, Value, value2, Result)
  values
  (1, 10, 25, null),
  (1, 10, 25, null),
  (1, 10, 25, null),
  (2, 10, 5, null),
  (2, 10, 5, null),
  
  select * from #A
  </code></pre>
  
  <p>So, I would like to take Value away from "value2", if there are left overs, <strong>just update it to 0</strong>, for <strong>next row i would take those "left overs"</strong> and use them to take away from, with next Value</p>
  
  <p>I would like to get results like this...</p>
  
  <pre><code>ID  Value     value2    Result
   1    10        25        0
   ----------------------------
   1    10        25        0
   ----------------------------
   1    10        25        5
   ----------------------------
   2    10        5         5
   ----------------------------
   2    10        5         10
  </code></pre>
  
  <p>So as you can see with ID 1 ... it would be:</p>
  
  <pre><code>10 - 25 = 0
  10 - 15 = 0
  10 - 5  = 5
  </code></pre>
  
  <p>I hope you understand what I am trying to do here ... let me know if I can explain more ... </p>