• You are asking SQL Server experts how to programmatically solve a math problem. I think you would be better served by asking mathematicians and programmers of procedural languages.

    Outside of SQL, a simplified English example of your mathematical problem appears to be:

    {

    Given the following set of 8 numbers {2,3,1,4,2,5,2,1}, how many combinations of their sums can be split into two sets that both equal the sum of them all divided by 2?

    }

    I believe you are referring to Partition Problem (http://en.wikipedia.org/wiki/Partition_problem).

    On that page I see at least 5 different algorithms proposed. Those algorithms are presented in a variety of procedural languages. I suggest you translate each algorithms' procedural language into SQL and test. The translation is where a mathematician or a programmer familiar with one of the languages will be able to help you. T-SQL syntax is well documented in both MSDN or TechNet. Your testing should include the maximum number of elements in your set (which I assume will be greater than 8). Do not expect this code to be fast. Do expect one of the algorithms to perform faster will a small number of elements, while a different algorithm to perform faster for a larger number of elements.

    But before making the translation effort, I believe you need to be aware that SQL is not optimized for procedural code and you need to be aware that SQL is not a procedural language. Instead of being procedural, SQL is interpreted (by various database engine manufacturers, such as Microsoft's SQL Server engine). The SQL standard defines what the results will be for a give SQL statement. The standard does NOT define HOW the expected results MUST be obtained. That does not mean you cannot use procedural statements in SQL (you can). That means SQL is not optimized for handling procedural statements. That means you really should be looking at a procedural language for a procedural answer, before you consider SQL. Still, this is one reason why SQL Server Supports CLR. As I said, you have at least 5 different choices, and even when CLR is used, you should test each algorithm under a variety of costs (if you really must accomplish this in SQL Server).