aragorn
aragorn

Reputation: 187

SQL help is needed in merging two tables

I have a [Date] Column in [Table1] and a column [Actions] in [Table2]

[Table1] looks like:

Date
----
2018-01-01
2018-01-02
...
2018-02-28
2018-03-01
...
2018-12-31

[Table2] looks like:

Actions
-------
Action1
Action2
Action3
...

I want to create a [Table3] to look like this:

Date       | Actions 
--------------------
2018-01-01 | Action1
2018-01-02 | Action1
...
2018-02-28 | Action1
2018-03-01 | Action1
...
2018-12-31 | Action1
2018-01-01 | Action2
2018-01-02 | Action2
...
2018-02-28 | Action2
2018-03-01 | Action2
...
2018-12-31 | Action2
2018-01-01 | Action3
2018-01-02 | Action3
...
2018-02-28 | Action3
2018-03-01 | Action3
...
2018-12-31 | Action3
...

In other words to assign in all dates all actions. Is there an SQL command to do that? Thanks.

Upvotes: 0

Views: 32

Answers (2)

Zaynul Abadin Tuhin
Zaynul Abadin Tuhin

Reputation: 31993

below query also will give Cartesian product output

SELECT * FROM table1,table2

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270091

You seem to want a Cartesian product. In SQL, you do this using CROSS JOIN:

select t1.date, t2.action
from t1 cross join
     t2
order by t2.action, t1.date;

How you save this into a table depends on the database you are using. Typically it would either be create table table3 as select . . . or select . . . into table3 from . . ..

Upvotes: 3

Related Questions