• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 241
  • Last Modified:

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.
       
0
JakeyCakes
Asked:
JakeyCakes
  • 4
  • 4
  • 2
  • +1
1 Solution
 
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
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
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
 
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

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now