Fin correct SQL Syntax

I have two tables A and B with column fields "Phone Number". BTW, the link of the two tables is the field UNITID
What Id like to prove are:

1) Find IN Table A, check all Phone numbers that match with Table B

2} Find all Phone numbers in Table A that do not match in Table B or present in Table A and not in Table B

3) Also find all Phone Numbers in table B that do not match in Table A
zachvaldezAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Nitin SontakkeDeveloperCommented:
May be you should have posted the table structure and some sample data and the expected output. That way it is lot easier / quicker for us to just put the query together and determine if expectations are met or not.

In the absence of such a data, here is a real crud query which you wish to / have to modify.

select 
   coalesce(a.[UnitId], b.[UnitId]) [UnitId]
  ,coalesce(a.[PhoneNumber], b.[PhoneNumber]) [PhoneNumber]
  ,case when a.[UnitId] is not null and b.[UnitId] is not null and a.[PhoneNumber] is not null and b.[PhoneNumber] is not null then 'Yes' else 'No' end [Matched In Both]
  ,case when a.[UnitId] is not null and b.[UnitId] is null and a.[PhoneNumber] is not null and b.[PhoneNumber] is null then 'Yes' else 'No' end [Missing in B]
  ,case when a.[UnitId] is null and b.[UnitId] is not null and a.[PhoneNumber] is null and b.[PhoneNumber] is not null then 'Yes' else 'No' end [Missing in A]
from tableA a
full outer join tableB b on a.[UnitId] = b.[UnitId] and a.[PhoneNumber] = b.[PhoneNumber]

Open in new window


Hope it helps!
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
Bill PrewIT / Software Engineering ConsultantCommented:
Are you looking for 1 query, or three query's?


»bp
0
awking00Information Technology SpecialistCommented:
1) Find IN Table A, check all Phone numbers that match with Table B
select phone_number from A intersect select phone_number from B

2} Find all Phone numbers in Table A that do not match in Table B or present in Table A and not in Table B
select phone_number from A except select phone_number from B

3) Also find all Phone Numbers in table B that do not match in Table A
select phone_number from B except select phone_number from A
0
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

awking00Information Technology SpecialistCommented:
The previous was for SQL Server. Depending on your DBMS (e.g. Oracle), you might have to substitute minus instead of except
0
zachvaldezAuthor Commented:
DBMS is SQL Server
0
zachvaldezAuthor Commented:
I'm looking for 3 queries.
0
zachvaldezAuthor Commented:
I did not see an inner join by UNITID between table A and B added in the select statement.
0
zachvaldezAuthor Commented:
here's example of the table structure

Table A                                       Table B
UnitID                                           UnitID
UnitType                                     Address
PrinName                                    City
Phone                                              Phone
WEbAddress
0
zachvaldezAuthor Commented:
Is it possible to just create a view and query against it?
0
awking00Information Technology SpecialistCommented:
Are there any UnitID values that exist in one table and not the other? Would still like to see some sample data and your expected output.
0
zachvaldezAuthor Commented:
there is a need to join 2 tables
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
SQL

From novice to tech pro — start learning today.