title: Sum a column and get the first row in Transact Sql
body: <p>I have a table MOUVEMENTS which has 3 columns :</p>
  
  <pre><code>ID     IDREF     NUMBER
  1      1         5
  2      1         3
  3      1         4
  4      1         2
  5      2         1
  </code></pre>
  
  <p>I'd like to fetch the rows of this table with that constraints :</p>
  
  <ul>
  <li>IDREF = 1</li>
  <li>Ordered by ID ASC</li>
  <li>and the X first SUM of NUMBER (by IDREF)</li>
  </ul>
  
  <p>I imagine that we will first calculate the SUM. And then we will restrict with that column</p>
  
  <pre><code>ID     IDREF     NUMBER   SUM
  1      1         5        5
  2      1         3        8
  3      1         4        12
  4      1         2        2
  5      2         1        1
  </code></pre>
  
  <p>In this case, if we want to have 11, we will take the two first column + the third and we will change the number to have a coherent value.</p>
  
  <p>So the result awaited :</p>
  
  <pre><code>ID     IDREF     NUMBER   SUM
  1      1         5        5
  2      1         3        8
  3      1         3        11
  </code></pre>
  
  <p>Please note the change in the third line on the NUMBER and SUM column.</p>
  
  <p>Do you know how to achieve that ?</p>
  
  <p>This query should work from sql 2000 to 2008 R2</p>