create table products
(
product_id varchar(20) ,
cost int
);
insert into products values ('P1',200),('P2',300),('P3',500),('P4',800);
create table customer_budget
(
customer_id int,
budget int
);
insert into customer_budget values (100,400),(200,800),(300,1500);
with cte as (
select *, sum(cost) over(order by product_id) as cum_sum from products
)
,cte1 as(
select c.customer_id,c.budget ,p.product_id
from customer_budget c
left join cte p on c.budget>=p.cum_sum
)
select customer_id,budget,group_concat(product_id) as product
from cte1
group by customer_id,budget
;
To embed this project on your website, copy the following code and paste it into your website's HTML: