I'm trying to build a sql query that will return a list of IDs that have a total sum, which is less than OR greater than a given value using the least number of items.
Here's an example of the table I'll be querying.
ID Value
-----------
226 2.3
331 3.1
25 1.5
28 1.5
29 1.2
52 5.2
38 3.5
Here it is sorted by Value asc.
ID Value
----------
29 1.2
25 1.5
28 1.5
226 2.3
331 3.1
38 3.5
52 5.2
Example A :
If my value is 6, I would expect the query to return IDs 29, 25, 28 and 226. 1.2 + 1.5 + 1.5 + 2.3 = 6.5
Example B :
If my value is 19, I would expect the query to return all of the IDs (29, 25, 28, 226, 331, 38, 52).
1.2 + 1.5 + 1.5 + 2.3 + 3.1 + 3.5 + 5.2 = 18.3
I've tried the suggested answer found here: SQL select elements where sum of field is less than N
However, that's not giving me exactly what I need since it only returns IDs that add up to LESS than the set value. Also it is assuming that the ID is ascending which isn't the case when I sort by asc value.
Is this even possible within a sql statement? or would I have to do a procedure/function to accomplish this task?