title: Get all records until the sum of column less than or equal a given value and category
body: <p>This is ALMOST answered by this thread <a href="https://stackoverflow.com/questions/21824383/query-to-get-all-records-until-the-sum-of-column-less-than-or-equal-a-value">Query to get all records until the sum of column less than or equal a value</a></p>
  <p>Now the difference is that  I do have a <strong>CATEGORY</strong> column</p>
  <pre><code>slno     item       price   category
  1        item1      1000    cat1
  2        item2      2000    cat1
  3        item3      3000    cat2
  4        item4      4000    cat2
  5        item5      5000    cat3
  6        item6      6000    cat3
  </code></pre>
  <p>Now the <strong>GIVEN value</strong> is <strong>10000</strong> and <strong>GIVEN categories</strong> are <strong>cat1, cat2, cat3</strong></p>
  <p>Results should be, since these data met the condition of &lt;= 10000</p>
  <pre><code>slno   item   price   category
  1      item1  1000    cat1
  3      item3  3000    cat2
  5      item5  5000    cat3
  </code></pre>
  <p>the current query is this,</p>
  <pre><code>SELECT slno, item, price, category
  
   FROM
  (
    SELECT slno, item, price, category,
    (
      SELECT SUM(price)
        FROM table1
       WHERE slno &lt;= t.slno
    ) total
      FROM table1 t
  ) q
   WHERE total &lt;= 10000
   ORDER BY slno
  </code></pre>