title: How to SELECT top N rows that sum to a certain amount?
body: <p>Suppose:</p>
  
  <pre><code>MyTable
  --
  Amount
  1
  2
  3
  4
  5
  </code></pre>
  
  <p><code>MyTable</code> only has one column, <code>Amount</code>, with 5 rows. They are not necessarily in increasing order.<br>
  How can I create a function, which takes a <code>@SUM INT</code>, and returns the <strong>TOP N rows</strong> that sum to this amount?<br>
  So for input 6, I want </p>
  
  <pre><code>Amount
  1
  2
  3
  </code></pre>
  
  <p>Since 1 + 2 + 3 = 6. 2 + 4 / 1 + 5 won't work since I want <strong>TOP N ROWS</strong><br>
  For 7/8/9/10, I want</p>
  
  <pre><code>Amount
  1
  2
  3
  4
  </code></pre>
  
  <p>I'm using MS SQL Server 2008 R2, if this matters.  </p>