Select rows from T such that sum(T.quantity) is greater than an argument
If you don't need the "optimal fill", you could use a window function and something like this:

select *
from (
  select id, name, price, category, 
         sum(price) over (order by price, date_created) as so_far
  from my_table
  where price &gt; 40 
    and category = 'buy' 
) t
where so_far &lt;= 12;


If you are mixing "groups" of orders, you might need a partition by clause as well.
Change the order by inside the window function to meet your ordering criteria.