468,242 Members | 1,587 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,242 developers. It's quick & easy.

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

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
2 995
ck9663
2,878 Expert 2GB
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
1,134 Expert 1GB
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.

Similar topics

3 posts views Thread by Paradigm | last post: by
6 posts views Thread by das | last post: by
reply views Thread by kermitthefrogpy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.