title: Handling negative values with sql
body: <p>I have a data set that lists the date and quantity of future stock of products. Occasionally our demand outstrips our future supply and we wind up with a negative future quantity. I need to factor that future negative quantity into previous supply so we don't compound the problem by overselling our supply.</p>
  
  <p>In the following data set, I need to prepare for demand on 10-19 by applying the negative quantity up the chain until i'm left with a positive quantity:</p>
  
  <pre><code>"ID","SKU","DATE","SEASON","QUANTITY"
  "1","001","2012-06-22","S12","1656"
  "2","001","2012-07-13","F12","1986"
  "3","001","2012-07-27","F12","-283"
  "4","001","2012-08-17","F12","2718"
  "5","001","2012-08-31","F12","-4019"
  "6","001","2012-09-14","F12","7212"
  "7","001","2012-09-21","F12","782"
  "8","001","2012-09-28","F12","2073"
  "9","001","2012-10-12","F12","1842"
  "10","001","2012-10-19","F12","-12159"
  </code></pre>
  
  <p>I need to get it to this:</p>
  
  <pre><code>"ID","SKU","DATE","SEASON","QUANTITY"
  "1","001","2012-06-22","S12","1656"
  "2","001","2012-07-13","F12","152"
  </code></pre>
  
  <p>I have looked at using a while loop as well as an outer apply but cannot seem to find a way to do this yet. Any help would be much appreciated. This would need to work for sql server 2008 R2.</p>
  
  <p>Here's another example:</p>
  
  <pre><code>"1","002","2012-07-13","S12","1980"
  "2","002","2012-08-10","F12","-306"
  "3","002","2012-09-07","F12","826"
  </code></pre>
  
  <p>Would become:</p>
  
  <pre><code>"1","002","2012-07-13","S12","1674"
  "3","002","2012-09-07","F12","826"
  </code></pre>