Wednesday, January 19, 2011

SQL Server: Finding Duplicate Records

Recently I came across a situation where we need to findout the duplicate records based on certain criteria.

Problem: To findout duplicate users based on firstname and lastname
Solution:
Asssume tableA has below definition and already having data.
tableA(UserID,FirstName,LastName,LoginID,Password,Status)

SELECT a.UserID,a.FirstName,a.LastName,a.LoginID,a.Password
FROM tableA a
WHERE (
SELECT COUNT(b.FirstName+''+b.LastName) FROM tableA b
 WHERE a.FirstName=b.FirstName AND a.LastName=b.LastName AND b.Status <> 'delete'
) > 1
AND a.Status <> 'delete'
Order By FirstName,LastName

Please add your valueble comments and if any other alternative ways to get the same result.

Reference: DP (http://dptechnicalblog.blogspot.com/ )

No comments:

Post a Comment