Urgent - duplicate problem

Hi,

I have a problem (unfortunately due to an error on my part) where I urgently need help:

I have a table called client that has fields, clientid, refcode, firstnames and Surname

the problem is that I have duplicate records of married clients but I have to be careful not to accidentally delete legitimate records - this best shown below:

ClientID         Refcode         FirstNames        Surname
 1                 Dand /S         David                Dand
 2                 Dand /S         Jean                 Dand
 3                30624            David                Dand
 4                30624/w        Jean                 Dand
 6               2056             Jake                  Field
 7               30790/w        Carol                Field
 8               30790             Robert             Field
 9               Field /S        Carol                Field
 10             Field /S           Robert             Field
 11             30892            Andrew             Goldthorpe
 12             30892W          Kathryn             Goldthorpe
 13             Goldthorpe /s         Kathryn             Goldthorpe

As you can see, client 6 is a legitimate record with the same surname as one set of duplicate records. I want to delete the records that  end with '/S'

so the end result will look like:


 3                30624            David                Dand
 4                30624/w        Jean                 Dand
 6               2056             Jake                  Field
 7               30790/w        Carol                Field
 8               30790             Robert             Field
 11             30892            Andrew             Goldthorpe
 12             30892W          Kathryn             Goldthorpe


 I really do need help correcting my error.
Thanks.
       
JakeyCakesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Aneesh RetnakaranDatabase AdministratorCommented:
SELECT T.ClientID ,       T.Refcode,         T.FirstNames,        T.Surname
FROM Client T
INNER JOIN
(SELECT Min(ID) FROM Clients GROUP BY RefCode ) A
On A.ClientID = T.ClientID
0
pootle_flumpCommented:
Hi

Is that literally your data? If so:

DELETE
FROM MyTable
WHERE Refcode LIKE '%/S'

???
0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
delete client
where RefCode like '%/S'
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Aneesh RetnakaranDatabase AdministratorCommented:
Sorry leave my previous post ...

0
JakeyCakesAuthor Commented:
aneeshattingal,


if I wanted to delete the duplicate records to get the result I stated in my initial, should I modify the code to look like the one below?


DELETE
FROM Client T
INNER JOIN
(SELECT Min(ID) FROM Clients GROUP BY RefCode ) A
On A.ClientID <> T.ClientID
0
JakeyCakesAuthor Commented:
"if I wanted to delete the duplicate records to get the result I stated in my initial,"

should read

f I wanted to delete the duplicate records to get the result I stated in my initial post,
0
JakeyCakesAuthor Commented:
angellIII and pootle_flump, not all the refcodes ending in /S are incorrect or duplicates...

e.g.

 1                 Dand /S         David                Dand
 2                 Dand /S         Jean                 Dand
 3                30624            David                Dand
 4                30624/w        Jean                 Dand
 6               2056             Jake                  Field
 7               30790/w        Carol                Field
 8               30790             Robert             Field
 9               Field /S        Carol                Field
 10             Field /S           Robert             Field
 11             30892            Andrew             Goldthorpe
 12             30892W          Kathryn             Goldthorpe
 13             Goldthorpe /s         Kathryn             Goldthorpe
 14             Jacks /S                Jenny                Jacks
0
JakeyCakesAuthor Commented:
and no, I should have stated it is an example of the data, not that it is literally the data.
0
pootle_flumpCommented:
Do you have any duplicated forenames and surnames for different people e.g. John Smith aged 41 from NY and John Smith aged 22 from CA?
0
pootle_flumpCommented:
Well - if the answer is "no" then perhaps:

DELETE
FROM Client A
WHERE ClientID IN (SELECT Min(ClientID) FROM Client B WHERE A.FirstNames = B.FirstNames AND A.Surnames = B.Surnames)

If the answer is yes then you are a bit pickled.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
pootle_flumpCommented:
and...
some general advice re dupes. Some things other things to consider include table size and (associated) downtime
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6256
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.

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.