Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:33:04+00:00 2026-05-16T03:33:04+00:00

I have 2 databases that have the same structure, but different data. Both are

  • 0

I have 2 databases that have the same structure, but different data. Both are SQL 2005.

I am trying to find which of the Persons in Database A, exist in Database B. My best opportunity for match is to match on FirstName and LastName.

I only want to bring back a list of:

DatabaseA.Person
DatabaseB.Person

Where:
1. I want all records from DatabaseA, even if there is not a match in Database B.
2. I only want records from DatabaseB where the FirstName/LastName match only one record in DatabaseB.

I have written a query, where I group by, but since I need to see more data than FirstName and LastName, I cannot bring it back without grouping it – which gives me many duplicates. What kind of query should I be using? Do I need to use a cursor?

Here is my query now, which sort of works – except I’m getting results for duplicates in DatabaseB and all I want to know about Database B is when FirstName/LastName matches to one distinct record and no others. My objective is to get a list of people that I know are the same person in 2 databases so that I can build a dictionary list of department code mappings between employees.

    select 
count(DatabaseAEmployee.id) as matchcount
, DatabaseAPerson.id as DatabaseAPersonid
, DatabaseAEmployee.DeptCode DatabaseADeptCode
, DatabaseAPerson.firstname as DatabaseAfirst
, DatabaseAPerson.lastname as DatabaseAlast
, DatabaseBPerson.id as DatabaseBPersonid
, DatabaseBEmployee.DeptCode as DatabaseBDeptCode
, DatabaseBPerson.firstname as DatabaseBfirst
, DatabaseBPerson.lastname as DatabaseBlast
, DatabaseAPerson.ssn as DatabaseAssn
, DatabaseBPerson.ssn as DatabaseBssn
, DatabaseAPerson.dateofbirth as DatabaseAdob
, DatabaseBPerson.dateofbirth as DatabaseBdob

FROM [DatabaseA].[dbo].Employee DatabaseAEmployee
LEFT OUTER JOIN [DatabaseA].[dbo].Person DatabaseAPerson 
 ON DatabaseAPerson.id = DatabaseAEmployee.id
LEFT OUTER JOIN [DatabaseB].[dbo].Person DatabaseBPerson
 ON 
 DatabaseAPerson.firstname = DatabaseBPerson.firstname 
 AND
 DatabaseAPerson.lastname = DatabaseBPerson.lastname 
LEFT OUTER JOIN [DatabaseB].[dbo].Employee DatabaseBEmployee 
 on DatabaseBEmployee.id = DatabaseBPerson.id
group by 
DatabaseAPerson.firstname
, DatabaseAPerson.lastname
, DatabaseAPerson.id
, DatabaseAEmployee.DeptCode
, DatabaseBPerson.id
, DatabaseBEmployee.DeptCode
, DatabaseBPerson.firstname
, DatabaseBPerson.lastname
, DatabaseBPerson.ssn
, DatabaseAPerson.ssn
, DatabaseBPerson.dateofbirth
, DatabaseAPerson.dateofbirth

Here’s what I’m trying now, but I’m getting duplicates on the left side:

with UniqueMatchedPersons (Id, FirstName, LastName)
as (
select 
    p2.ID, p2.FirstName, p2.LastName
from 
    [DatabaseA].[dbo].[Employee] p1 
INNER JOIN [DatabaseA].[dbo].[Person] p2 on p1.id = p2.id
    inner join [DatabaseB].[dbo].[Person] p3
        on p2.FirstName = p3.FirstName and p2.LastName = p3.LastName
INNER JOIN  [DatabaseB].[dbo].[Employee] p4
on p3.id = p4.id

group by p2.ID, p2.FirstName, p2.LastName
having count(p2.ID) = 1

)

select p1.*, p2.*
from DatabaseA.dbo.Person p1
inner join UniqueMatchedPersons on p1.ID = UniqueMatchedPersons.ID
left outer join DatabaseB.dbo.Person p2 
    on p1.FirstName = p2.FirstName and p1.LastName = p2.LastName
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:33:04+00:00Added an answer on May 16, 2026 at 3:33 am

    Try this:

    SELECT id,FirstName,Lastname 
    FROM   dba.Persons
    UNION
    SELECT b.id,b.FirstName,b.LastName 
    FROM   dbb.Persons as b
    INNER JOIN dba.Persons as a
    ON b.FirstName = a.FirstName AND b.LastName = a.LastName
    

    If you want to get all from A and only those from B that DON’T have a match (which would make more sense to me) i’d use this:

    SELECT id,FirstName,Lastname 
    FROM dba.Persons
    UNION
    SELECT b.id,b.FirstName,b.LastName 
    FROM dbb.Persons as b
    LEFT OUTER JOIN dba.Persons as a
    ON b.FirstName = a.FirstName AND b.LastName = a.LastName
    WHERE a.id is null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 483k
  • Answers 483k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer YES and NO are BOOLs, which is not an Objective-C… May 16, 2026 at 7:06 am
  • Editorial Team
    Editorial Team added an answer You are looking for: FireQuery http://firequery.binaryage.com/ To do it your… May 16, 2026 at 7:06 am
  • Editorial Team
    Editorial Team added an answer This is not possible without jailbreaking the phone. However, you… May 16, 2026 at 7:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.