Joining two tables

  • Thread starter Thread starter IgorM
  • Start date Start date
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
 
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
 
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
 
Back
Top