Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
iloveorangesoda
Starting Member
30 Posts |
Posted - 2008-01-10 : 12:40:25
|
I have the following script below which checks my employee table for duplicate employees based on surname, forename, dateofbirth and NINumber and inserts the duplicates into a temp table i.e. @temptable. I now want to change how NINumber is matched. For example i want to now say:match on the 1st 8 characters of NI Number where the NI Number does not start with TN.For NI Numbers that start with TN match on all 9 chars.Can anyone help with the syntax?code is as follows:DECLARE @temptable table(f_id int primary key identity(1,1), NhssNumber int, Surname varchar(18), Forename varchar(11), DateOfBirth datetime, NINumber varchar(9), MaxNhssNumber int)INSERT INTO @temptableSELECT a.NhssNumber, CAST(a.Surname as varchar(18)) as Surname, CAST(a.Forename as varchar(11)) as Forename, CONVERT(char(8),a.DateOfBirth,112) as DateOfBirth, CAST(a.NINumber as varchar(9)) as NINumber, NULL FROM dbo.t_Employee a JOIN (SELECT Surname, Forename,DateOfBirth,NINumber FROM dbo.t_Employee GROUP BY Surname, Forename,DateOfBirth,NiNumber HAVING COUNT(*) > 1) b ON a.Surname = b.Surname AND a.Forename = b.Forename AND a.DateOfBirth = b.DateOfBirth AND a.NiNumber = b.NiNumber ORDER BY a.Surname, a.Forename,a.DateOfBirth,a.NiNumberselect * from @temptable |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-10 : 12:53:41
|
quote: Originally posted by iloveorangesoda I have the following script below which checks my employee table for duplicate employees based on surname, forename, dateofbirth and NINumber and inserts the duplicates into a temp table i.e. @temptable. I now want to change how NINumber is matched. For example i want to now say:match on the 1st 8 characters of NI Number where the NI Number does not start with TN.For NI Numbers that start with TN match on all 9 chars.Can anyone help with the syntax?code is as follows:DECLARE @temptable table(f_id int primary key identity(1,1), NhssNumber int, Surname varchar(18), Forename varchar(11), DateOfBirth datetime, NINumber varchar(9), MaxNhssNumber int)INSERT INTO @temptableSELECT a.NhssNumber, CAST(a.Surname as varchar(18)) as Surname, CAST(a.Forename as varchar(11)) as Forename, CONVERT(char(8),a.DateOfBirth,112) as DateOfBirth, CAST(a.NINumber as varchar(9)) as NINumber, NULL FROM dbo.t_Employee a JOIN (SELECT Surname, Forename,DateOfBirth,NINumber FROM dbo.t_Employee GROUP BY Surname, Forename,DateOfBirth,NiNumber HAVING COUNT(*) > 1) b ON a.Surname = b.Surname AND a.Forename = b.Forename AND a.DateOfBirth = b.DateOfBirth AND ((a.NiNumber = b.NiNumber AND LEFT(a.NiNumber,2)='TN') OR (LEFT(a.NiNumber,8)= LEFT(b.NiNumber,8) AND LEFT(a.NiNumber,2)<>'TN')) ORDER BY a.Surname, a.Forename,a.DateOfBirth,a.NiNumberselect * from @temptable
|
 |
|
iloveorangesoda
Starting Member
30 Posts |
Posted - 2008-01-11 : 05:33:39
|
ThanksBut I am still having a problem with returning matches on the 1st 8 characters of NI Number where the NI Number does not start with TN? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-01-11 : 05:39:56
|
quote: Originally posted by iloveorangesoda ThanksBut I am still having a problem with returning matches on the 1st 8 characters of NI Number where the NI Number does not start with TN?
Is filetering not working as expected? can you give some example of case where its not working and also your expected result? |
 |
|
iloveorangesoda
Starting Member
30 Posts |
Posted - 2008-01-11 : 07:03:47
|
if i had the following in records in my table:Forename Surname DateofBirth NINumberMark Jones 27/08/1980 TN1234567Mark Jones 27/08/1980 TN1234567Paul Smith 25/02/1970 AB123456PPaul Smith 25/02/1970 AB123456XThese should all be returned as duplicates i.e. match on all 9 chars when TN, but match on 8 chars if not.What is happening with current script is that it doesn't recognize the two paul smith's as duplicates when it should.Thanks |
 |
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2008-01-11 : 07:07:45
|
1. get the subquery right...this is key.This bit needs to be in the "Grouping clause" and the select statement ...from your original (subquery) code.case LEFT(a.NiNumber,2) when 'TN' then a.NiNumber else LEFT(a.NiNumber,8) end2. then get the join for the insert right you may need 2 queries (unioned together)...one dealing with the TN and one dealing with the non-TN people. |
 |
|
iloveorangesoda
Starting Member
30 Posts |
Posted - 2008-01-11 : 07:17:01
|
ThanksCould you show me what you mean as i am a novice at sql. Cheers |
 |
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2008-01-11 : 12:21:28
|
SELECT Surname, Forename,DateOfBirth, case LEFT(a.NiNumber,2) when 'TN' then a.NiNumber else LEFT(a.NiNumber,8) endFROM dbo.t_EmployeeGROUP BY Surname, Forename,DateOfBirth, case LEFT(a.NiNumber,2) when 'TN' then a.NiNumber else LEFT(a.NiNumber,8) endHAVING COUNT(*) > 1something like this??? the syntax of the CASE statement may need to be improved/verified...(I don't use it much myself)....if it's not correct, remove the GROUP BY clause 1st to get the basic syntax right and then copy what ever you get working into the GROUP BY clause. |
 |
|
|
|
|
|
|