Return all possible combinations of values on columns in SQL

后端 未结 8 2480
轮回少年
轮回少年 2020-11-28 10:44

How do I return a list of all combinations of values in 2 columns so they are new rows in T-SQL?

e.g.

Col1, Col2
----  ----
1     2
1     4
1     5
<         


        
8条回答
  •  情话喂你
    2020-11-28 10:49

    You could cartesian join the table to itself, which would return all combinations of both columns.

    select 
        distinct
        t1.Col1,
        t2.Col2
    from 
        MyTable t1,
        MyTable t2
    

提交回复
热议问题