Join 2 different tables in SQL Code

Hello Experts,

I need help in joining 2 tables and keeping all records even if the record does not exist in both tables.  Here are my example tables:

TBLA
Col1      Col2      Col3      
1000      1      1      
1002      1      1      
1004      1      1      
                  
TBLB                  
Col1      Col2      Col3      Col4
1000      1      1      1
1002      1      1      1
1003      1      1      1

My final output should like this:
Col1      Col2      Col3      Col4
1000      1      1      1
1002      1      1      1
1003      1      1      1
1004      1      1      0

I am stumped with this problem in both oracle and MS SQL Server queries

Thanks in advance for your help.
LVL 1
fb1990Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vikas GargBusiness Intelligence DeveloperCommented:
Hello,

You can try this

SELECT Col1,Col2,Col3,MAX(Col4) Col4 FROM
(
SELECT *,0 Col4 FROM TBLA
UNION
SELECT * FROM TBLB
) TBL
GROUP BY Col1,Col2,Col3

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
this should do it:
select  isnull(t1.col1, t2.col1) col1
   , coalesce( t1.col2, t2.col2, 0) col2
   , coalesce( t1.col3, t2.col3, 0) col3
   , coalesce( t1.col4, t2.col4, 0) col4
  from table1 t1
 full outer join table2 t2
   on t1.Col1   = t2.col1

Open in new window

if you wonder what the t1 and the t2 are about, read up this article
http://www.experts-exchange.com/articles/11135/Why-should-I-use-aliases-in-my-queries.html
1
PortletPaulfreelancerCommented:
tiny suggestions, no points please

SELECT Col1,Col2,Col3,MAX(Col4) Col4
FROM (
      SELECT * FROM TBLB
      UNION ALL
      SELECT *,0 Col4 FROM TBLA
      ) TBL
GROUP BY Col1,Col2,Col3
;

select  isnull(t1.col1, t2.col1) col1
   , coalesce( t1.col2, t2.col2, 0) col2
   , coalesce( t1.col3, t2.col3, 0) col3
   , coalesce( t2.col4, 0) col4
  from tbla t1
 full outer join tblb t2
   on t1.Col1   = t2.col1
1
fb1990Author Commented:
You continue to wow me Paul.  Thank you Sir!  I heed your request by awarding the points to Guy and Vikas,  Thanks all for making life easy!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.