Reputation: 1
I have a table
declare @table table(t varchar(50), d varchar(50), activ varchar(10), groupid int, rownum int)
insert into @table values('ALK','ceri', '0.2',1,1)
insert into @table values('ALK','criz', '24',1,2)
insert into @table values('EGFR','erlo', '2',2,3)
insert into @table values('EGFR','gefi', '57',2,4)
insert into @table values('EGFR','ibru', '5.6',2,5)
insert into @table values('EGFR','ceri', '900',2,6)
insert into @table values('EGFR','cetu', 'NULL',2,7)
insert into @table values('EGFR','afat', '10',2,8)
insert into @table values('EGFR','lapa', '10.8',2,9)
insert into @table values('EGFR','pani', 'NULL',2,10)
insert into @table values('ERBB2','pert', 'NULL',3,11)
insert into @table values('ERBB2','tras', 'NULL',3,12)
insert into @table values('ERBB2','lapa', '9.2',3,13)
insert into @table values('ERBB2','ado-', 'NULL',3,14)
insert into @table values('ERBB2','afat', '14',3,15)
insert into @table values('ERBB2','ibru', '9.4',3,16)
in output I need all combinations by groupid or t in format t,d,t,d,t,d,activ and so on then I will qualify best combinations.
Any help will be appreciated. This will show doctors optimum combination of drugs for cancer patients. The table is dynamic and different for every patient.
Thank you
Upvotes: 0
Views: 546
Reputation: 9134
I will use #TT to represent the table var since calling it @table may be a bit confusing I also changed the datatype of active to float
There are really 3 possible cross joins
-- #1 -- producing 256 rows
select * from #TT as T1
cross join #TT as T2
-- #2 -- produces 104 rows
select * from #TT as T1
cross join #TT as T2
where T1.GroupID = T2.GroupID
-- #3 -- produces 104
select * from #TT as T1
cross join #TT as T2
where T1.t = T2.t
The 1st is a true cross join on the whole table.
The 2nd and 3rd are cross joins on GroupID and t respectively, but they are identical since Group 1 represents T='ALK', etc. This is easily confirmed since a union of 2 & 3 3 also produces 104 rows
However, select * on a self join is silly as is obvious if you change select * to
select T1.*, '===', T2.*
You can see the columns on the left of '===' are the same as the columns to the right of '==='
Since GroupID is an integer I would write the cross join as
select T1.* from #TT as T1
cross join #TT as T2
where T1.GroupID = T2.GroupID
Now since the poster wanted to grouping based on the smallest total active, I think it makes sense to group the response by GroupID and T and D giving and report the sum of Activ and order by GroupID and sum(Activ)
-- #4 adding group by and sum -- 16 rows generated
select T1.groupid, T1.t, T1.d, sum(T1.activ) as SumActiv
from #TT as T1
cross join #TT as T2
where T1.groupid = T2.groupid
group by T1.t, T1.groupid, T1.d
order by groupid, sum(T1.Activ)
Now you are getting close except for the fact that no CROSS JOIN is needed at all
-- #5 remove the cross join
select T1.groupid, T1.t, T1.d, sum(T1.activ) as SumActiv
from #TT as T1
group by T1.t, T1.groupid, T1.d
When I remove the cross join portion of the query I get the exact same result. I think we finally have what is wanted, with the possible exception of removing all but the first row for each combination of GroupID and d
Upvotes: 0
Reputation: 12375
For all possible combinations, you would use CROSS JOIN:
SELECT * FROM table1 AS t1
CROSS JOIN table2 AS t2
on t1.ID = t2.ID
Keep in mind this gives a O(n^2)
result set, likely to be huge for large sets of data.
Upvotes: 1