Solved

How to compare two different columns in two different databases

Posted on 2014-10-31
6
29 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 69

Expert Comment

by:Scott Pletcher
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:Scott Pletcher
Scott Pletcher 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable TLS1.0 on Win 2012 server 7 114
How can I use this function? 3 35
Database Availability Group Distribution 9 49
Set a variable value in SQL Procedure 3 26
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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