This question is an extension of the question here Query to get all records until the sum of column less than or equal a value
For the table: items
Use a cumulative sum and then join:
join
select v.*, i.* from (select i.*, sum(price) over (order by slno) as running_price from items i ) i join values v on i.running_price <= v.value order by v.id, i.slno;
2.1m questions
2.1m answers
60 comments
57.0k users