drop table emp1
drop table emp2
drop table #temp1
drop table #temp2
drop table #results
/*
-- If you do not have a numbers table, create one:
create table Numbers (Num int identity(1,1) primary key clustered)
insert Numbers DEFAULT VALUES
while scope_identity() < 2000 insert Numbers DEFAULT VALUES
*/
create table Emp1 (FullName varchar(50))
create table Emp2 (FullName varchar(50))
insert Emp1 select 'Susan Smith' insert Emp2 select 'Suzy Smith'
insert Emp1 select 'George Lucas' insert Emp2 select 'George Lucaz'
insert Emp1 select 'Bronson, James' insert Emp2 select 'Jimmy Bronson'
insert Emp2 select 'Jason Bronson'
insert Emp1 select 'Dick Jones' insert Emp2 select 'Jones, Richard'
insert Emp2 select 'Jones,David'
insert Emp1 select ' Dan Jones' insert Emp2 select 'Jones, Don '
insert Emp1 select 'Brian Duke' insert Emp2 select 'Duck, Bryan'
insert Emp1 select 'Olaf Olafson' insert Emp2 select 'Olafsen, Olfa'
update Emp1 set fullname = ltrim(rtrim(fullname)) where fullname like ' %' or fullname like '% '
update Emp2 set fullname = ltrim(rtrim(fullname)) where fullname like ' %' or fullname like '% '
while exists (select * from Emp1 where FullName like '% %')
update Emp1 set FullName = Replace(FullName, ' ', ' ') where FullName like '% %'
while exists (select * from Emp2 where FullName like '% %')
update Emp2 set FullName = Replace(FullName, ' ', ' ') where FullName like '% %'
while exists (select * from Emp1 where FullName like '%,[^ ]%')
update Emp1 set FullName = Replace(Replace(FullName, ',', ', '), ', ', ', ') where FullName like '%,[^ ]%'
while exists (select * from Emp2 where FullName like '%,[^ ]%')
update Emp2 set FullName = Replace(Replace(FullName, ',', ', '), ', ', ', ') where FullName like '%,[^ ]%'
select
E.FullName,
Pos = N.Num - Len(Replace(Left(E.FullName, N.Num), ' ', '')) + 1,
Word = Replace(Substring(E.FullName, N.Num, charindex(' ', E.FullName + ' ', N.Num) - 1), ',', ''),
Sound = dbo.SoundexAlphaFunction(Replace(Substring(E.FullName, N.Num, charindex(' ', E.FullName + ' ', N.Num) - 1), ',', ''))
into
#Temp1
from
Emp1 E
inner join Numbers N ON Len(E.FullName) >= N.Num AND Substring(' ' + E.FullName, N.Num, 1) = ' '
select
E.FullName,
Pos = N.Num - Len(Replace(Left(E.FullName, N.Num), ' ', '')) + 1,
Word = Replace(Substring(E.FullName, N.Num, charindex(' ', E.FullName + ' ', N.Num) - 1), ',', ''),
Sound = dbo.SoundexAlphaFunction(Replace(Substring(E.FullName, N.Num, charindex(' ', E.FullName + ' ', N.Num) - 1), ',', ''))
into
#Temp2
from
Emp2 E
inner join Numbers N ON Len(E.FullName) >= N.Num AND Substring(' ' + E.FullName, N.Num, 1) = ' '
select
Name1 = E1.FullName,
Pos1 = E1.Pos,
Name2 = E2.FullName,
Pos2 = E2.Pos,
Score = 100.
INTO
#Results
from
#Temp1 E1
INNER JOIN #Temp2 E2 ON E1.Sound = E2.Sound
insert #results
select
Name1 = E1.FullName,
Pos1 = E1.Pos,
Name2 = E2.FullName,
Pos2 = E2.Pos,
Score = Sum(50. / Case when Len(E1.Sound) > len(e2.Sound) then Len(e1.Sound) else len(e2.Sound) end)
from
#Temp1 E1
INNER JOIN Numbers N1 ON Len(E1.Sound) >= N1.Num
INNER JOIN (
#Temp2 E2
INNER JOIN Numbers N2 ON Len(E2.Sound) >= N2.Num
) ON Substring(E1.Sound, N1.Num, 1) = Substring(E2.Sound, N2.Num, 1)
group by
E1.FullName,
E1.Pos,
E2.FullName,
E2.Pos
select
name1,
name2,
totalscore = sum(score) / max(case when pos1 > pos2 then pos1 else pos2 end)
from
#results
group by
name1,
name2
order by
name1,
totalscore desc