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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Combinations

Author  Topic 

pravin14u
Posting Yak Master

246 Posts

Posted - 2008-01-14 : 03:09:36
Hi,

I have a table with 2 columns (S.No,Name) with rows
1,Raja
2,Ramu
3,Rane

I need to generate all 2 pair combinations of the names(i,e)

(Raja -Ramu)
(Raja -Rane)
(Ramu -Rane)

Can some one give me a generalized query for this problem?

Thanks,

Prakash.P

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-14 : 03:31:25
CROSS JOIN with inequality filter.

SELECT t1.Name, t2.Name
FROM Table1 AS t1
CROSS JOIN Table1 AS t2
WHERE t1.Sno < t1.Sno



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-14 : 03:31:48
This is also known as "triangular join".



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-01-14 : 03:37:53
quote:
Originally posted by Peso

CROSS JOIN with inequality filter.

SELECT t1.Name, t2.Name
FROM Table1 AS t1
CROSS JOIN Table1 AS t2
WHERE t1.Sno < t2.Sno



E 12°55'05.25"
N 56°04'39.16"




Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-01-14 : 04:34:27
Thank you.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -