I'd like to find duplicate rows in a table based on matching two conditions. First condition, match on the data in the LastName field. Second condition, match on just the first three characters of data in the FirstName field.
For example, these two rows should be selected:
LastName FirstName
Williams Robert
Williams Robbie
I attempted to build on RedFilter's solution: Finding duplicate rows in SQL Server but getting 0 records returned. Here's my query;
SELECT a.ObjGUID, a.LastName, a.FirstName, a.EmailAddress, ac.duplicateCount
FROM [Users].[dbo].[Known_Dupes_4] a
INNER JOIN (
SELECT
LastName, FirstName, COUNT(*) AS duplicateCount
FROM [Users].[dbo].[Known_Dupes_4]
GROUP BY
LastName, FirstName
HAVING
COUNT(*) > 1
) ac ON (UPPER(a.LastName) = UPPER(LTRIM(RTRIM(ac.LastName))) AND LEFT (UPPER(LTRIM(RTRIM(a.FirstName))),3) LIKE LEFT (UPPER(LTRIM(RTRIM(ac.FirstName))),3))
ORDER BY
a.LastName, a.FirstName
Thanks
updated - this appears to work
SELECT a.EmployeeID, a.LastName, a.FirstName, a.EmailAddress, ac.duplicateCount
FROM [Users].[dbo].[Known_Dupes_4] a
INNER JOIN (
SELECT
LEFT(FirstName,3) as firstNameShort, COUNT(*) AS duplicateCount, LastName
FROM [Users].[dbo].[Known_Dupes_4]
GROUP BY
LastName, LEFT(FirstName,3)
HAVING
COUNT(*) > 1
) ac ON (a.LastName) = ac.LastName AND LEFT (a.FirstName,3) LIKE LEFT (ac.firstNameShort,3)
ORDER BY a.LastName, a.FirstName