Solved

How to compare two different columns in two different databases

Posted on 2014-10-31
6
25 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

14 Experts available now in Live!

Get 1:1 Help Now