title: Postgres: limit by the results of a sum function
body: <pre><code>CREATE TABLE inventory_box (
   box_id varchar(10),
   value   integer
  );
  
  INSERT INTO inventory_box VALUES ('1', 10), ('2', 15), ('3', 20);
  </code></pre>
  
  <p>I prepared a <a href="http://sqlfiddle.com/#!15/87aa4/1" rel="nofollow">sql fiddle</a> with the schema.</p>
  
  <p>I would like to select a list of inventory boxes with combined value of above 20</p>
  
  <pre><code>possible result 1. box 1 + box 2 (10 + 15 &gt;= 20) 
  </code></pre>
  
  <p>Here is what I am doing right now:</p>
  
  <pre><code>SELECT * FROM inventory_box LIMIT 1 OFFSET 0;
  -- count on the client side and see if I got enough
  -- got 10
  SELECT * FROM inventory_box LIMIT 1 OFFSET 1;
  -- count on the client side and see if I got enough
  -- got 15, add it to the first query which returned 10
  -- total is 25, ok, got enough, return answer
  </code></pre>
  
  <p>I am looking for a solution where the scan will stop as soon as it reaches the target value</p>