Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Duplicate Records Problem

Status
Not open for further replies.

jpicks

MIS
Oct 11, 2002
158
US
I am having difficulty trying to delete duplicate records in a table. The table has two columns "Social Security Number" and "Date of Birth". The problem is that there is no unique identifier on the table. The records are all unique when you combine ssn and dob, but there are multiple ssn with different dob.

I need to clean the data up so that there are no duplicate ssn in the table. The duplicate ssn all have different dob and we don't care what dob gets paired with the ssn.

Any suggestions?

I am completely out of ideas and the boss needs it done by 9:00 tomorrow.

Any help or suggestions would be greatly appreciated
 
Hi,
You can select distinct record to a temporary table and delete the whole records. After that, insert back to the original table. You can use the DTS to do this task if you want it be done every month or week.(Chong)

 
Here is a complete script that should work for you with slight modification.

--Create a temp table with distinct
--rows from the original table
Select distinct *
Into #temp
From YourTable As t
Where dob=
--select max dob. you may want to use min?
(Select max(dob)
From YourTable Where ssn=t.ssn)

--Add identity to the temp table
Exec("Alter table #temp add RecID int identity")

--Delete rows with duplicate ssn and dob
Delete
From #temp
Where RecID >
(Select Min(RecID)
From #temp t
Where t.ssn=#temp.ssn)

--Remove rows from original table
Truncate table YourTable
--Use delete if you are not an SA or DBO
--Delete YourTable

--Insert unique rows into original table
Insert Yourtable
Select * From #temp

--Add a unique index or primary key to
--prevent duplicates in the future

--Cleanup
drop table #temp Terry L. Broadbent - DBA
Computing Links:
faq183-874 contains "Suggestions for Getting Quick and Appropriate Answers" to your questions.
 
Hi there,

are you trying to delete them through enterprise manager? If so SQL has a problem when there is no key and there are duplicate records. Try running a delete query in query analyser and this should solve your problem.

Thanks and good luck!
Transcend
 

Thanks to all for your replies!

Terry, your script worked great. I only needed to use part of it to copy the clean data to a new table. We needed to clean up the data before importing it into a new system we are implementing this morning.

Here is the part of Terry's script that I used to copy only unique data over to a temp table:

Select distinct *
Into #temp
From YourTable As t
Where dob=
--select max dob. you may want to use min?
(Select max(dob)
From YourTable Where ssn=t.ssn)

Again. Thanks to all. You made my day!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top