How can I get the permutations of elements from two subqueries in T-SQL?

Let's say I have two sub queries:

SELECT Id AS Id0 FROM Table0

=>

Id0
---
1
2
3

and

SELECT Id AS Id1 FROM Table1

=>


Id1
---
4
5
6

      

How to combine them to get the query result:

Id0 Id1
-------
1   4
1   5
1   6
2   4
2   5
2   6
3   4
3   5
3   6

      

+1


a source to share


3 answers


Try the following:

SELECT A.Id0, B.Id1
FROM (SELECT Id AS Id0 FROM Table0) A, 
     (SELECT Id AS Id1 FROM Table1) B

      



Gregoire

+1


a source


Cartesian union, union without a join condition

select id0.id as id0, id1.id as id1 
from id0, id1

      

Alternatively, you can use the CROSS JOIN syntax if you prefer



select id0.id as id0, id1.id as id1 
from id0 cross join id1

      

you can order your request if you want to get a specific order, from your example it looks like you want

select id0.id as id0, id1.id as id1
from id0 cross join id1 order by id0.id, id1.id

      

+1


a source


SELECT Table0.Id0, Table1.Id1 FROM Table0 Full Join table 1 1 = 1

+1


a source







All Articles