views:

66

answers:

3

I have 2 tables that are similar but not the same so a union is not a possibility. I need to combine the tables bearing in mind there's about 40 columns where only 20 are common to both. Any ideas on the best approach?

Table1

ActivityCategory    ActivityType   Nationality   Language
---------------------------------------------------------
Communication       Telephone      French        French
Meeting             Session        British       English


Table2

ActivityCategory    ActivityType   Nationality   Employment
-----------------------------------------------------------
Communication       Fax            American      Employed


Combined Table

ActivityCategory    ActivityType   Nationality   Language   Employment
----------------------------------------------------------------------
Communication       Telephone      French        French
Meeting             Session        British       English
Communication       Fax            American                 Employed
+1  A: 

Union what is possible to union, and later on join the rest of the collumns?

Pretty possible if you will need to do it only one time. I guess that even with a view its possible to do it.

George
+5  A: 

Do a UNION but first you have to make the tables same in structure.

In the SELECT for Table1, add Employment as a NULL value column

In the SELECT for Table2, add Language as a NULL value column

SELECT ActivityCategory, ActivityType, Nationality, Language, NULL AS Employment
FROM Table1
UNION
SELECT ActivityCategory, ActivityType, Nationality, NULL AS Language, Employment
FROM Table1
Raj More
Hi Raj, I had thought of that but there's actually about 40 columns with only about 20 common ones. I was hoping for an easy way out but I guess I might just have to take the painful approach unless you know another way!?
Mitch
+1, it will work with 40 columns too, if not this approach then you can work from two different result sets
KM
I'm gonna go with this approach. Thanks Raj and KM
Mitch
+1  A: 

Raj's suggestion is good, but you will probably want to do a UNION ALL and not a UNION.

From BOL,

UNION ALL incorporates all rows into the results. This includes duplicates. If not specified, duplicate rows are removed.

Using UNION pays the price to guarantee no duplicates, but in your case, it sounds like you won't have duplicates anyway.

Rob Garrison