Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to compare two different columns in two different databases

Posted on 2014-10-31
6
Medium Priority
?
31 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 1000 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 1000 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 49

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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 video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

719 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