Link to home
Start Free TrialLog in
Avatar of Aleks
AleksFlag for United States of America

asked on

sql to display and delete duplicates

I am trying to create a recordset that displays duplicates and also then a SP to delete them.

The table name is "Atts"

Fields are:

Select * from Atts
where caseid and userid (ARE DUPLICATES).

In other words, I want to see the rows in which caseid and userid are the same in row # 1 and row # 2, althought I want to display that only once, not 2 or 3 times.

Then to clean it up I want to run a SP to delete the duplicate items (or 3 or 4) and leave only one.

Example

Name     |   caseid   |    userid   |   other

Aleks       |  1001      |    323        |   XXXXX
Aleks       |  1001       |    323        |  XXXXX
Maria       |   1001            |    433         |
John          |  4324     |  23233  |   YYYY

The first two are duplicates, I want to display that result once, then the SP to delete the exceeding and leave just one. The example shows one but it would have 4 or 5 entries with the same values under "Caseid"  and "userid"


Delete from Atts
where caseid and userid (ARE DUPLICATE)  --- But only the exceeding ones, so to leave one entry

Hope that makes sense.
ASKER CERTIFIED SOLUTION
Avatar of Subramani N
Subramani N

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

Ill try the SP above see how that works. Will get back to you this weekend.
Avatar of Aleks

ASKER

I would also need the sql to display the dups. I tried:

SELECT
   DISTINCT caseid,userid
FROM
   Atts
GROUP BY
   caseid,userid
HAVING
   COUNT(1) > 1


But that displays the unique ones. I need user to review how many records will be deleted, then to test Ill substract and make sure it was done properly
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

Looks great ! Ill take it the SP will work so Ill assign the points already.
Avatar of Aleks

ASKER

I've requested that this question be closed as follows:

Accepted answer: 0 points for amucinobluedot's comment #a40306698
Assisted answer: 250 points for Subramani N's comment #a40306671
Assisted answer: 250 points for Subramani N's comment #a40306718

for the following reason:

:)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Aleks

ASKER

That was not a direct answer to my specific query.  Before I had a chance to even start working on it I got an answer to my specific request, which is much more useful than a link to a page I could have googled. Not sure if that should get any points. Ill leave that to the EE moderator.
Avatar of Aleks

ASKER

Sorry I thought I had already done this.