?
Solved

sql to display and delete duplicates

Posted on 2014-09-05
12
Medium Priority
?
115 Views
Last Modified: 2014-09-09
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.
0
Comment
Question by:amucinobluedot
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 1

Accepted Solution

by:
Subramani N earned 2000 total points
ID: 40306671
Here is the SP.
CREATE PROCEDURE SP_DEL_DUPS
AS
BEGIN

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

WITH TMP_DUPS AS(
   SELECT caseid, userid, rownum = ROW_NUMBER()OVER(PARTITION BY caseid, userid ORDER BY caseid, userid)
   FROM Atts
)
DELETE FROM TMP_DUPS WHERE rownum > 1

END
GO

Open in new window

0
 
LVL 1

Assisted Solution

by:Subramani N
Subramani N earned 2000 total points
ID: 40306682
Here is another example.

http://support.microsoft.com/kb/139444
0
 

Author Comment

by:amucinobluedot
ID: 40306689
Ill try the SP above see how that works. Will get back to you this weekend.
0
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 

Author Comment

by:amucinobluedot
ID: 40306698
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
0
 
LVL 1

Assisted Solution

by:Subramani N
Subramani N earned 2000 total points
ID: 40306718
Try this.
SELECT
   caseid,userid,count(1)-1 as 'RowsToBeDeleted'
FROM
   Atts
GROUP BY
   caseid,userid
HAVING
   COUNT(1) > 1

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 40306730
Looks great ! Ill take it the SP will work so Ill assign the points already.
0
 

Author Comment

by:amucinobluedot
ID: 40306766
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:

:)
0
 
LVL 1

Assisted Solution

by:Subramani N
Subramani N earned 2000 total points
ID: 40306741
You dont have to raise a Close request. You just click on the Comment and select as solution and provide the points, it will be closed automatically and moved from the queue.
0
 
LVL 1

Assisted Solution

by:Subramani N
Subramani N earned 2000 total points
ID: 40306767
Please accept the answer and submit.
0
 

Author Comment

by:amucinobluedot
ID: 40306797
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.
0
 

Author Closing Comment

by:amucinobluedot
ID: 40311982
Sorry I thought I had already done this.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question