Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

SQL Todas as combinações de round-robin possíveis entre duas tabelas


Aqui estão duas soluções para o problema
SELECT  t1.Name + ',' +  t2.Name AS NamesCombination
FROM Person t1
INNER JOIN Person t2
ON t1.Name < t2.Name

OU (Oracle 11i R2+)
WITH NamesCombination AS 
( 
    SELECT  1 AS Cntr
            ,Name 
            , CAST(Name AS VARCHAR(MAX))AS NamesCombinations
    FROM Person 
    UNION ALL 
    SELECT  
        nc.Cntr+1
        ,p.Name 
        ,nc.NamesCombinations + ',' +  CAST(p.Name AS VARCHAR(MAX))                      
    FROM Person AS p JOIN NamesCombination nc ON p.Name < nc.Name
    WHERE nc.Cntr < 2  
)
SELECT NamesCombinations
FROM NamesCombination
WHERE Cntr  = 2