By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,843 Members | 2,324 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,843 IT Pros & Developers. It's quick & easy.

How to do UNION of two query results when both results do not have same columns

P: 1
Hi,
Please help me with SQL code that would do Union of two query results NOT having same columns. For example, first result returns columns A,B,C and D and second result returns columns A,B and C. I do not want rows with duplicate values for A, B and C columns.

Result 1:
A B C D
1 2 3 4
1 3 4 5

Result 2:
A B C
1 2 3
7 8 9

Final Desired Result:
A B C D
1 2 3 4
1 3 4 5
7 8 9 NULL

Thanks,
B. Senjalia
Oct 20 '08 #1
Share this Question
Share on Google+
2 Replies


ck9663
Expert 2.5K+
P: 2,878
You might need two steps to do this.

1. UNION the table using just those similar columns. You still have to use all column on your first SELECT.

2. Do a JOIN to get the remaining columns. Use all the columns that are the same to relate the unionize table and the tables that you will be getting these value from.

Good luck!

-- CK
Oct 21 '08 #2

Delerna
Expert 100+
P: 1,134
or depending on the complexity of what you are doing
this may be sufficient ?
Expand|Select|Wrap|Line Numbers
  1.  select a,b,c,d from result1
  2.  union all
  3.  select a,b,c,null from result2
  4.  
Oct 21 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.