I need the random rows to be displayed till the target reaches it should reach the target if there is any possible combination is there.
Id price
1 20
2 30
3 40
4 15
5 10
.......200+ rows
If the target is 30 it should return with
id price
1 20
5 10
or
id price
2 30
If the target exceeds the sum or no combinations --120 in this case it should return till last maximum
Id price
1 20
2 30
3 40
4 15
5 10
If the target is less than the sum --5 in this case it should return nothing
Id price
what i tried
select t.*(select q.*,sum(q.price) over(order by newid())as total from Orders q)t
where t.total<=45
i want rows till the exact target is reached if there is no possible combinations of getting the exact target then only it has to return till the nearest value to the target
this query is giving me the rows not exactly all the time