create table dbo.events(eventname varchar(50))
create table dbo.eventsmass(eventmassid int not null, eventname varchar(50), mass int )
create table dbo.partitions(eventid1 int , eventid2 int, partitionvalue int)
insert into dbo.events
select 'big bang'
union
select 'big bang'
union
select 'big bang'
union
select 'big bang'
union
select 'big bang'
insert into dbo.eventsmass
select 1, 'big bang', 100
union
select 2, 'big bang', 10000
union
select 3, 'big bang', 55
union
select 4, 'big bang', 33
union
select 5, 'big bang', 12
insert into dbo.partitions
select a.eventmassid, b.eventmassid, (SQUARE(a.mass) + SQUARE(b.mass)) partitionvalue
from eventsmass a
cross apply eventsmass b
where a.eventname = b.eventname
and a.eventmassid <> b.eventmassid
select top 1 partitionvalue top1, * from partitions order by partitionvalue asc
select partitionvalue sortdesc, * from partitions order by partitionvalue asc
drop table dbo.events
drop table dbo.eventsmass
drop table dbo.partitions
Another thing , i think you miss understood my problem .
The solution that you mention is giving me the sum of 2 values only , but i need the sum of all permutation within every possible combination for each event name .
example -
select 1, 'big bang', 10
union
select 2, 'big bang', 20
union
select 3, 'big bang', 30
union
select 4, 'big bang', 40
Here i have 4 mass values , so i have 7 combinations ( (2^(n-1)) -1 ) - the "n" is 4 .
I have 7 combination for splitting the sum values to 2 groups , i have 4 different values for "big bang" event - 10,20,30,40 , so i have ( (2^(n-1)) -1 ) - which is 7 possibilities of splitting this 4 numbers to 2 groups ,
If i have 10 in Group1 so the other values (20,30,40) should be in Group2 .