I have a table with Products, Quantity and Weight. I need to find the heavier products that reach a certain value. Example:
Table:
Product Qty Weight
PEN 100 50
PEN 10 30
PEN 80 20
PEN 250 20
PEN 1000 10
PAPER 150 80
PAPER 1500 20
Query: Find PEN that reach maximum 200 Qty ordered by Weight.
Result:
PEN 100 50
PEN 10 30
PEN 80 20
EDIT:
The result was that because: 100+10+80 < 200.
If you’re on SQL 2012, you can use this sort of approach for a running total:
http://sqlfiddle.com/#!6/6157b/4