I am trying to identify potential duplicate customers in my database based on the last 4 of the SSN, last name and DOB. The stored procedure I have written does identify potential duplicates but it lists them in one row - I am trying to split into separate rows for reporting reasons.
My T-SQL looks like:
DECLARE
@StartDate DATE = '1/1/2017',
@EndDate DATE = '3/1/2017';
SELECT DENSE_RANK() OVER (ORDER BY c.socialSecurityNumber) AS [SSNRanking] ,
ROW_NUMBER() OVER (PARTITION BY c.socialSecurityNumber ORDER BY c.socialSecurityNumber) AS [RowNumb] ,
c.socialSecurityNumber AS [SSN],
c.id AS [CustomerID] ,
c.firstName AS [FirstName] ,
c.lastName AS [lastName] ,
c.birthDate [birthdate] ,
c.createDate AS [CreateDate] ,
c2.socialSecurityNumber AS [DupSSN] ,
c2.id AS [DupCustomerID] ,
c2.firstName AS [DupFirstName] ,
c2.lastName AS [DupLastName] ,
c2.birthDate AS [DupBirthDate] ,
c2.createDate AS [DupCreateDate]
FROM dbo.Customers AS [c]
INNER JOIN dbo.Customers AS [c2] ON ( SUBSTRING(c.socialSecurityNumber,6,4) = SUBSTRING(c2.socialSecurityNumber,6,4) AND c.birthDate = c2.birthDate AND c.lastName = c2.lastName AND c.id <> c2.id )
LEFT JOIN dbo.CustomerAddresses AS [CA] ON c.id = CA.customerID
LEFT OUTER JOIN dbo.Common_Orders AS [co] ON co.customerID = c.id
WHERE
c.customerStatusTypeID <> 'M'
AND C2.customerStatusTypeID <> 'M'
AND c.mergedTo IS NULL
AND c2.mergedTo IS NULL
AND CAST(co.orderDate AS DATE) >= @StartDate
AND CAST(co.orderDate AS DATE) <= @EndDate
AND c.id = 1234439
GROUP BY c.socialSecurityNumber ,
c.id ,
c.firstName ,
c.lastName ,
c.birthDate ,
c.createDate ,
c2.socialSecurityNumber ,
c2.id ,
c2.firstName ,
c2.lastName ,
c2.birthDate ,
c2.createDate
ORDER BY CAST(c.socialSecurityNumber AS INT) ASC;
And my data set looks like:
SSNRanking RowNumb SSN CustomerID FirstName lastName birthdate CreateDate DupSSN DupCustomerID DupFirstName DupLastName DupBirthDate DupCreateDate
1 1 000009915 1234439 GREG GARRETT 1900-01-01 2014-02-25 000009915 1166084 ADAM GARRETT 1900-01-01 2013-08-29
In this particular instance, I have two users with the same last 4 of the SSN, same last name and same DOB - but different first names.
How can I make these two records appear on separate lines? Ideally, I would like to see:
SSNRanking RowNumb SSN CustomerID FirstName lastName birthdate CreateDate
1 1 000009915 1234439 GREG GARRETT 1900-01-01 2014-02-25
1 2 000009915 1166084 ADAM GARRETT 1900-01-01 2013-08-29
But I am not sure how I can accomplish this when joining to the same table. Suggestions?
I'm linking to a script that creates the two tables in question and inserts sample data. Hopefully that is acceptable: SQL Script