Copying duplicate records to a seperate table

I have a table named Customers. I want to identify records in the Customer table
that have the same CustomerId. In otherwords I want to identify the duplicate records.
What I need to do, is to copy the duplicate records to a table named "Duplicates".
Can someone help me to write the query? Below is my Customers table with the duplicate
records. And below that is the Duplicates table, after it has been populated based upon
a query from the Customers table.


Customers

CustomerId   CustFullName      EntryDate           CustIsActive
233456700    Jim Carslon           2/10/2012              1
233456700    Jim Carlson           5/06/2013              1
544667890    Troy Salano          3/05/2011              1
111789999    Mary Dropsi         4/07/2014              1
111789999    Mary Dropsi         5/06/2009              1
111789999    Mary Dropsi          5/06/2013             1
566897004    Travis Goldman    7/10/2012             0
566897004    Travis Goldman    2/12/2015             0
666789000    Lori Mazelton       4/10/2008              1





Duplicates

233456700    Jim Carslon         2/10/2012              1
233456700    Jim Carlson         5/06/2013              1
111789999    Mary Dropsi        4/07/2014              1
111789999    Mary Dropsi        5/06/2009              1
111789999    Mary Dropsi        5/06/2013              1
LVL 2
brgdotnetcontractorAsked:
Who is Participating?
 
Walter RitzelSenior Software EngineerCommented:
In a rough mode, this is it.
insert into duplicates
select *
from customers
where CustomerId in (select CustomerId from customers group by CustomerId having count(1) > 1)

Open in new window

0
 
awking00Commented:
Given the records you show as duplicates, I think the above proposed solution just needs to add -
where CustIsActive = 1
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.