Joining two tables

I

IgorM

Hi

I want to join two tables (by UNION ALL). In order two identify the source
table I'd like to add a column that would contain a table name (typed in in
the query).
So for instance:
Table1
Name, Surname, DateOfBirth

Table2
Name, Surname, DateOfBirth, Address

JoinedTable
Name, Surname, DateOfBirth, Address, Source

I started with:

Select Table2.Name, Table2.Surname, Table2.DateOfBirth, Table2.Address
FROM Table2
UNION ALL
Select Table1.Name, Table1.Surname, Table1.DateOfBirth, NULL
FROM Table1

But I just don't know how to add the fifth column (source) so for the data
that come from table one it would contain (Table1) and from the second
(Table2).

Kind regards
IgorM
 
D

Daryl S

IgorM -

Is this what you want?

Select Table2.Name, Table2.Surname, Table2.DateOfBirth, Table2.Address,
"Table 2" AS Source
FROM Table2
UNION ALL
Select Table1.Name, Table1.Surname, Table1.DateOfBirth, NULL, "Table 1" AS
Source
FROM Table1
 
J

John W. Vinson

But I just don't know how to add the fifth column (source) so for the data
that come from table one it would contain (Table1) and from the second
(Table2).

Just use a text literal:

Select Table2.Name, Table2.Surname, Table2.DateOfBirth, Table2.Address,
"Table 2" AS Source
FROM Table2
UNION ALL
Select Table1.Name, Table1.Surname, Table1.DateOfBirth, NULL, "Table 1"
FROM Table1
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top