Ok, I would like to ask you about something similiar to question: Cross Join without duplicate combinations I have 8 players of table football. We want to play all possible combination games. So for generating all possible teams (2 players) I can use this solution (in my opinion 28 teams are correct):
select distinct
case when a.id<=b.id then a.id else b.id end as p1,
case when a.id<=b.id then b.id else a.id end as p2
from
scores.players a join scores.players b on a.id!=b.id
but how can I generate all possible games between all possible teams without duplicates? I don't know how compare enought all columns. I tried to use this query and result is 420 combinations, but in my opinion is too much:
select distinct
t1.p1 as t1p1,
t1.p2 as t1p2,
t2.p1 as t2p1,
t2.p2 as t2p2
from
(select distinct
case when a.id<=b.id then a.id else b.id end as p1,
case when a.id<=b.id then b.id else a.id end as p2
from
scores.players a join scores.players b on a.id!=b.id) t1
join
(select distinct
case when a.id<=b.id then a.id else b.id end as p1,
case when a.id<=b.id then b.id else a.id end as p2
from
scores.players a
join scores.players b on a.id!=b.id) t2 on (t1.p1!=t2.p1 and t1.p1!=t2.p2 and t1.p2!=t2.p1 and t1.p2!=t2.p2)