Sqlserver
 sql >> Base de Dados >  >> RDS >> Sqlserver

Retorna todas as combinações possíveis de valores em colunas no SQL


Assumindo pelo menos SQL 2005 para o CTE:
;with cteAllColumns as (
    select col1 as col
        from YourTable
    union
    select col2 as col
        from YourTable
)
select c1.col, c2.col 
    from cteAllColumns c1 
        cross join cteAllColumns c2 
    where c1.col < c2.col
    order by c1.col, c2.col