Pete Willis
Pete Willis

Reputation: 3

SQL Query - Combine 2 tables into 1 with unique Values

I am trying to write a select query to combine 2 tables into a single query taking the columns from 1 and combining them with 1 column from the other.

My 2 source tables are as follows:

dbCalendar
Year | Month
------------
2017 | 1
2017 | 2
2017 | 3
2017 | 4

dbClients
Name
----
Client1
Client2
Client3

The Result I am trying to achieve is:

Year | Month | Name
----------------------
2017 | 1     | Client1
2017 | 2     | Client1
2017 | 3     | Client1
2017 | 4     | Client1
2017 | 1     | Client2
2017 | 2     | Client2
2017 | 3     | Client2
2017 | 4     | Client2
2017 | 1     | Client3
2017 | 2     | Client3
2017 | 3     | Client3
2017 | 4     | Client3

Am I thinking this is a nested query? Any help would be much appreciated?

Thanks

Upvotes: 0

Views: 90

Answers (3)

Rashid Ahmad Khan
Rashid Ahmad Khan

Reputation: 328

You can use either CROSS JOIN or by simply selecting both table name e.g.

SELECT * FROM dbCalendar CROSS JOIN dbClients;

OR

SELECT * FROM dbCalendar, dbClients

Both will return same result.

Upvotes: 1

indiri
indiri

Reputation: 332

A CROSS JOIN will join every row of the table to every row of the other table.

SELECT * FROM dbCalendar CROSS JOIN dbClients;

Upvotes: 1

Florin Toader
Florin Toader

Reputation: 337

What you are trying to achieve is called a "cartesian product", and it's very simple to do:

SELECT * FROM dbCalendar, dbClients

Upvotes: 1

Related Questions