I have data in a table which looks like the below data set.
I want to get a group of those items whose price sum is less than 10000.
CREATE TABLE Table1
(slno int, item varchar(10), price int);
INSERT INTO Table1
(slno, item, price)
VALUES
(1, 'item1', 1000),
(2, 'item2', 2000),
(3, 'item3', 3000),
(4, 'item4', 4000),
(5, 'item5', 5000),
(6, 'item6', 6000),
(7, 'item7', 10000),
(8, 'item8', 2000),
(9, 'item9', 8000),
(10, 'item10', 2500),
(11, 'item11', 9000),
(12, 'item12', 1000),
(13, 'item13', 2500),
(14, 'item14', 2500),
(15, 'item15', 2500);
My sql query looks like this:
SELECT slno, item,price
FROM
(
SELECT slno, item,price
(
SELECT SUM(price)
FROM Table1
WHERE slno<= t.slno
) total
FROM Table1 t
) q
WHERE total <= 1000
ORDER BY item
It's not giving the expected result though, it's giving only one set of records:
(1, 'item1', 1000),
(2, 'item2', 2000),
(3, 'item3', 3000),
(4, 'item4', 4000)
whereas I need it to give me something like this:
1ST SET
(1, 'item1', 1000),
(2, 'item2', 2000),
(3, 'item3', 3000),
(4, 'item4', 4000)
2ND SET
(7, 'item7', 10000),
@GordonLinoff