I have some event ,that have several records , each record has value that represents Mass (m) .

I need to find the "minimum sum of the squares of the mass " from all the possible combinations from each pairs of Mass for this particular event .

I can create 2 partitions with all the possible combinations and then check the pair with the minimum sum.

for example - we have 3 values (can be much more ) , the partition will look like this -

P1 = v1+v2 P2 = v3

P1 = v1+v3 P2 = v2

P1 = v2+v3 P2 = v1

and then we check which pair has the minimum sum of squares of mass value .

Any ideas how to built those partition in SQL ? or how to solve the min sum problem in a different way ?