Wednesday, March 7, 2012

How to find duplicates

Hi
I assume that some of you do this on a regular basis, so I hope you can
help.
We're currently in the process of merging 2 databases into 1. Therefore I
need to do some clean up of the data, and get rid of duplicate records in
some tables. It might be very simple to do this with some TSQL code, but I
just can't figure out to do it.
It will be fine just to test on one column in the table, and what I'd like
so get is some code that can show me all the records where there's more than
one record with the same value in that column in the table.
I hope that some of you can guide me in the right direction...
Regards
Steen
Here's an example from Pubs. Authors with duplicate last names:
SELECT A. au_id, A.au_lname, A.au_fname
FROM Authors AS A
JOIN
(SELECT au_lname
FROM Authors
GROUP BY au_lname
HAVING COUNT(*)>1) AS B
ON A.au_lname = B.au_lname
David Portas
SQL Server MVP
|||Thanks a lot David. That seems to be what I needed.
Regards
Steen
David Portas wrote:
> Here's an example from Pubs. Authors with duplicate last names:
> SELECT A. au_id, A.au_lname, A.au_fname
> FROM Authors AS A
> JOIN
> (SELECT au_lname
> FROM Authors
> GROUP BY au_lname
> HAVING COUNT(*)>1) AS B
> ON A.au_lname = B.au_lname
> --
> David Portas
> SQL Server MVP

No comments:

Post a Comment