I have a database for generate matches between teams of different company.
Example:
- Company A has teams: team1, team2, team3
- Company B has teams: comp1, comp2
- Company C has team: companyc
- Company D has teams: companyd1, companyd2
I have to generate the matches between teams, for example the team1 can't play with the team2 because is the same company.
I have this part, but other requirement is that they can play in consecutive order, example if the first match is the team1 vs comp1 the second match should be between company c vs companyd. How I can order so the same team doesn't have a consecutive match?
This is the query that I try
select Cotejos.id_pelea, Partido1, Nombre1, Partido2, Nombre2,
ronda = ROW_NUMBER() OVER(partition by Partido1 order by Partido1)
from Cotejos
order by ronda, newid()
give me the results in order of partido1 but partido 2 is repeated in the same ronda
for example the ordes is somehthing like this
Partido1 Nombre1 Partido2 Nombre2 ronda
----------- -------------------- ----------- -------------------- ----------
58 JJ Y LA ESTRELLA 2 1 AZTECA LAGUNA 3 1
123 LA JOYA 3 1 AZTECA LAGUNA 3 1
141 EL MILAGRO 2 1 AZTECA LAGUNA 1 1
2 GREGORIO GARCIA 3 CHICHO 1 1
5 GUARDADO 1 11 MA LUISA 2 1
119 GUARDADO 2 11 MA LUISA 3 1
14 RIELEROS Y CUMBRES 2 13 COMPADRES Y 28 DE OC 1
And I like something like this
Partido1 Nombre1 Partido2 Nombre2 ronda
----------- ----------- -------------------- ----------- -------------------
58 JJ Y LA ESTRELLA 2 1 AZTECA LAGUNA 3 1
2 GREGORIO GARCIA 3 CHICHO 1 1
5 GUARDADO 1 11 MA LUISA 2 1
123 LA JOYA 3 1 AZTECA LAGUNA 3 2
14 RIELEROS Y CUMBRES 2 13 COMPADRES Y 28 DE OC 2
119 GUARDADO 2 11 MA LUISA 3 2
141 EL MILAGRO 2 1 AZTECA LAGUNA 1 3