Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new table from two existing tables with every combination possibility

I have two temporary tables #a and #b both filled with integer values. Let's say they both contain 10 rows with values 1-10.

I want to create a third temporary table #c that contains every possible combination of a and b. So it would have 100 rows total with (1,1), (1,2) ... (10, 10). How would I go about doing this in SQL. The implementation I'm using is SQL Server 2012.

like image 330
Jay Sun Avatar asked Dec 09 '22 20:12

Jay Sun


2 Answers

Cross join will get all combinations

SELECT a.Col
, b.Col
FROM TableA a
CROSS JOIN TableB b
like image 78
Vinnie Avatar answered Dec 31 '22 21:12

Vinnie


I think you can just do

SELECT * INTO #c FROM #a,#b
like image 28
wizzardmr42 Avatar answered Dec 31 '22 21:12

wizzardmr42