Solved

How to compare two different columns in two different databases

Posted on 2014-10-31
6
23 Views
Last Modified: 2016-06-15
I need to compare records between to seperate database tables in two different databases.
Example:
Database A table1                                                 Database B table2
coulmn id                                                               column id
112233                                                                    112233
null                                                                          445566
778899                                                                     null
123456                                                                     123456

The quiery would return somthing like this:
Column ID Table 1 and Table 2         Is there an ID for both table 1 and 2       Is tere a id for table1    is there a in for table2
112233                                                            yes                                                                      yes                          yes
null                                                                   no                                                                      no                             yes
null                                                                   yes                                                        no                                                                  
123456                                                                 yes                                                                 yes                                 yes
0
Comment
Question by:newjeep19
6 Comments
 
LVL 69

Expert Comment

by:ScottPletcher
ID: 40415910
You need a "FULL OUTER JOIN":
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 250 total points
ID: 40415914
Try this:

With myTable as
(Select ID
from DatabaseA.dbo.table1
UNION
Select ID
from DatabaseB.dbo.table2
)

Select M.ID, IIF(T.ID IS NULL OR U.ID IS NULL,'no','yes') as IDT1And2, 
IIF(T.ID IS NULL,'no','yes') as IDTable1,
IIF(U.ID IS NULL,'no','yes') as IDTable2
from myTable M
LEFT JOIN DatabaseA.dbo.table1 T
ON M.ID = T.ID
LEFT JOIN DatabaseB.dbo.table2 U
ON M.ID = U.ID

Open in new window

0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 250 total points
ID: 40415917
SELECT
    ISNULL(t1.column_id, t2.column_id) AS column_id,
    CASE WHEN t1.column_id IS NULL OR t2.column_id IS NULL THEN 'No' ELSE 'Yes' END AS [ID for Table1 and Table2],
    CASE WHEN t1.column_id IS NULL THEN 'No' ELSE 'Yes' END AS [ID for Table1],
    CASE WHEN t2.column_id IS NULL THEN 'No' ELSE 'Yes' END AS [ID for Table2]
FROM dbA.table1 t1
FULL OUTER JOIN dbB.table2 t2 ON
    t2.column_id = t1.column_id


SELECT
    ISNULL(t1.column_id, t2.column_id) AS column_id,
    CASE WHEN t1.column_id IS NULL OR t2.column_id IS NULL THEN 'No' ELSE 'Yes' END AS [ID for Table1 and Table2],
    CASE WHEN t1.column_id IS NULL THEN 'No' ELSE 'Yes' END AS [ID for Table1],
    CASE WHEN t2.column_id IS NULL THEN 'No' ELSE 'Yes' END AS [ID for Table2]
FROM (
    SELECT '112233' AS column_id UNION ALL
    SELECT '778899' UNION ALL
    SELECT '123456'
) AS t1
FULL OUTER JOIN (
    SELECT '112233' AS column_id UNION ALL
    SELECT '445566' UNION ALL
    SELECT '123456'
) AS t2 ON t2.column_id = t1.column_id
0
 
LVL 48

Expert Comment

by:PortletPaul
ID: 40418803
Please note that IIF() exists in SQL Server 2012 and 2014 only

Personally I would always use SQL standard CASE expressions in preference to the MS centric IIF() syntax.
e.g.

IIF(T.ID IS NULL OR U.ID IS NULL,'no','yes') as IDT1And2

CASE WHEN T.ID IS NULL OR U.ID IS NULL THEN 'no' ELSE 'yes' END as IDT1And2
0
 

Author Comment

by:newjeep19
ID: 40547432
fixed
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now