Skip to content
Advertisement

Query to get correponding new col

Need to get team against team exculing the last x5

Teams 
    col1
    X1
    X2
    X3
    X4
    X5

Output Needed :
col1|col2

X1 X2 
X1 X3 
X1 X4 
X1 X5 
X2 X3 
X2 X4 
X2 X5
X3 X4 
X3 X5 
X4 X5

SQl query to get above output from a single column

Advertisement

Answer

You can get the combinations using a cross join. ie:

Select t1.Col1, t2.Col1
from Teams t1
cross join Teams t2
where t1.Col1 < t2.Col1; 

Note: Normally you would use the ID, in your sample it didn’t exist.

DBFiddle demo

EDIT: Sample with Id usage (Id is comparable like an Int):

Select t1.Col1 team1, t2.Col1 team2
from Teams t1
cross join Teams t2
where t1.Id < t2.Id
order by t1.Id, t2.Id;
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement