Find duplicates in SQL

2019-01-24 07:18发布

问题:

I have a large table with the following data on users.

social security number
name
address

I want to find all possible duplicates in the table where the ssn is equal but the name is not

My attempt is:

SELECT * FROM Table t1
WHERE (SELECT count(*) from Table t2 where t1.name <> t2.name) > 1

回答1:

A grouping on SSN should do it

SELECT
   ssn
FROM
   Table t1
GROUP BY
   ssn
HAVING COUNT(*) > 1

..or if you have many rows per ssn and only want to find duplicate names)

...
HAVING COUNT(DISTINCT name) > 1 

Edit, oops, misunderstood

SELECT
   ssn
FROM
   Table t1
GROUP BY
   ssn
HAVING MIN(name) <> MAX(name)


回答2:

This will handle more than two records with duplicate ssn's:

select count(*), name from table t1, (    
    select count(*) ssn_count, ssn 
    from table 
    group by ssn 
    having count(*) > 1
) t2
where t1.ssn = t2.ssn
group by t1.name
having count(*) <> t2.ssn_count


标签: sql tsql