我有两个结构相同但数据不同的数据库。两者都是 SQL 2005。

我正在尝试查找数据库 A 中的哪些人存在于数据库 B 中。我最好的匹配机会是匹配 FirstName 和 LastName。

我只想带回一个列表:

数据库A.Person 数据库B.人

地点: 1. 我想要数据库 A 中的所有记录,即使数据库 B 中没有匹配项。 2. 我只想要来自 DatabaseB 的记录,其中 FirstName/LastName 仅匹配 DatabaseB 中的一条记录。

我写了一个查询,我在其中分组,但由于我需要查看比 FirstName 和 LastName 更多的数据,我无法在不分组的情况下将其取回 - 这给了我很多重复项。我应该使用什么样的查询?我需要使用光标吗?

现在这是我的查询,它的工作原理 - 除了我在 DatabaseB 中得到重复的结果,我想知道的关于数据库 B 的所有信息是 FirstName/LastName 何时匹配到一个不同的记录而不是其他记录。我的目标是在 2 个数据库中获取我认识的同一个人的列表,以便我可以构建员工之间部门代码映射的字典列表。

    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 

这是我现在正在尝试的,但我在左侧得到了重复项:

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 

请您参考如下方法:

试试这个:

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 

如果你想从 A 中获取所有内容,并且只从 B 中获取不匹配的内容(这对我来说更有意义),我会使用这个:

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 


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!