Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
pssumesh2003
Starting Member
35 Posts |
Posted - 2012-04-30 : 03:15:23
|
hi allI have to find different combination of rows with a specified field,whose sum is given value.ie. if user give 400 & specified field manday , then we have to find all combination of rows having sum of manday is 400please help me |
|
pphillips001
Starting Member
23 Posts |
Posted - 2012-04-30 : 05:03:11
|
There are a couple of ways I can think of (there are probably more)You could use the HAVING clause.Or you could work out the sum in a query and then wrap another query around it with the MANDAY = 400 in the Where clause of the outer query.Hope this helps,Paul===========================There is no reality - only perspective. |
 |
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2012-04-30 : 06:56:34
|
If I understood your requirement correctly - that you want to find EVERY combination - this seems like a very hard problem to solve. Taking your example of combinations that sum up to 400, imagine that you have just 3 rows, each with value of 200. That yields you 3 combinations that will result in a sum of 400. (nCr in this simplified example).I can think of many ways to start attacking the problem, but nothing that is a clean, set-based solution. |
 |
|
pssumesh2003
Starting Member
35 Posts |
Posted - 2012-04-30 : 13:06:48
|
Dear sunitabeck,You understood my problem very well.Could u please tell any possible solution for this issue |
 |
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-04-30 : 14:20:35
|
What you are trying to solve is an NP-Complete problem. Those kinds of proplems are, generally, painful to solve. You can do some searching and see what others have come up with. But, I'm pretty sure you are going to have to implement a non-set-based solution.This isn't a complete solution, but about 2/3s of the way doen the page someone put some effort into creating a solution:http://stackoverflow.com/questions/6289314/how-to-get-rows-having-sum-equal-to-given-value |
 |
|
|
|
|
|
|