?
Solved

SP to delete duplicates

Posted on 2016-09-27
15
Medium Priority
?
88 Views
Last Modified: 2016-09-28
I have a table "Atts" which holds a few values, there maybe duplicate entries. I need a stored procedure that will remove any duplicate entries.

This is the table

SELECT AttsId ,
       FirmId ,
       CaseId ,
       UserId ,
       AccessLevel ,
       Xfer2Forms ,
       Ordernumatts FROM dbo.Atts

Open in new window


any records which are identical after running the SP only one should remain, any records without duplicates should stay as is.
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
  • 8
  • 3
  • 2
  • +2
15 Comments
 
LVL 33

Expert Comment

by:Big Monty
ID: 41818811
assuming that AttsID is where the duplicates are, try:

WITH CTE AS(
   SELECTAttsId ,
       FirmId ,
       CaseId ,
       UserId ,
       AccessLevel ,
       Xfer2Forms ,
       Ordernumatts
       RN = ROW_NUMBER()OVER(PARTITION BY AttsId ORDER BY AttsId )
   FROM dbo.Atts
)
DELETE FROM CTE WHERE RN > 1

Open in new window


try running the code directly in your database (maybe use a test table before anything). if that works, then stick in a SP and you'll be good to go
0
 

Author Comment

by:amucinobluedot
ID: 41818822
Actually the attsid is unique. The duplicate entries would be userid and caseid

So:

AttsId - Unique key
FirmId , -- irrelevant
CaseId , check 1
UserId ,  check 2
AccessLevel ,-- irrelevant
Xfer2Forms ,-- irrelevant
Ordernumatts -- irrelevant

I need to make sure that there is no more than one entry where userid and caseid are the same in more than one row.

if userid = 100
and caseid = 45

and there is another rown with those same values then its a duplicate. If they are different then not duplicate. The rest of the values are irrelevant. most of the time they are all the same for all rows other than the first one which is the ID and its unique and shouldn't be compared. Hope this clarifies things.
0
 
LVL 34

Expert Comment

by:Brian Crowe
ID: 41818928
Use Monty's RowNumber solution but PARTITION BY userid and caseid instead of attsid
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: 41818930
Not sure how to do that with two items I need to check and only when BOTH are the same is considered duplicate.
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 1000 total points
ID: 41818938
;WITH cte_dups AS (
    SELECT
        AttsId,
        ROW_NUMBER() OVER(PARTITION BY CaseId, UserId ORDER BY AttsId DESC) AS row_num
    FROM dbo.Atts
)
DELETE FROM cte_dups
WHERE row_num > 1
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41818939
Use below code.. Enjoy !!

WITH CTE AS
(
   SELECT
	   AttsId ,
       FirmId ,
       CaseId ,
       UserId ,
       AccessLevel ,
       Xfer2Forms ,
       Ordernumatts
       ROW_NUMBER()OVER(PARTITION BY UserId,CaseId ORDER BY (SELECT NULL)) rnk
   FROM dbo.Atts
)
DELETE FROM CTE WHERE rnk > 1

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41818948
I'll try both as soon as I get home ...
0
 

Author Comment

by:amucinobluedot
ID: 41818962
For some reason Pawan's script gives an error:

Msg 102, Level 15, State 1, Line 20
Incorrect syntax near '('.
Msg 102, Level 15, State 1, Line 20
Incorrect syntax near ')'.


the one from Scott seemed to work fine. Ill test some more.
0
 
LVL 29

Assisted Solution

by:Pawan Kumar
Pawan Kumar earned 1000 total points
ID: 41818965
comma was missing. chk this out.

;WITH CTE AS
(
   SELECT
	   AttsId ,
       FirmId ,
       CaseId ,
       UserId ,
       AccessLevel ,
       Xfer2Forms ,
       Ordernumatts,
       ROW_NUMBER()OVER(PARTITION BY UserId,CaseId ORDER BY (SELECT NULL)) rnk
   FROM dbo.Atts
)
DELETE FROM CTE WHERE rnk > 1

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41818966
Yep, that works. ill assing points to both. Thanks.
0
 

Author Closing Comment

by:amucinobluedot
ID: 41818967
Thanks to both.
0
 

Author Comment

by:amucinobluedot
ID: 41818979
I tried to create the SP in my Db but I get an error. I tried this:

CREATE PROCEDURE [dbo].[admin_removeduplicate_links]

;WITH cte_dups AS (
    SELECT
        AttsId,
        ROW_NUMBER() OVER(PARTITION BY CaseId, UserId ORDER BY AttsId DESC) AS row_num
    FROM dbo.Atts
)
DELETE FROM cte_dups
WHERE row_num > 1

Open in new window


Error:
Msg 195, Level 15, State 1, Procedure admin_removeduplicate_links, Line 5 [Batch Start Line 7]
'cte_dups' is not a recognized option.


I am using MS SQL 2008R2
0
 
LVL 29

Expert Comment

by:Pawan Kumar
ID: 41818981
Here it is

CREATE PROCEDURE [dbo].[admin_removeduplicate_links]
AS

BEGIN

	;WITH cte_dups AS (
		SELECT
			AttsId,
			ROW_NUMBER() OVER(PARTITION BY CaseId, UserId ORDER BY AttsId DESC) AS row_num
		FROM dbo.Atts
	)
	DELETE FROM cte_dups
	WHERE row_num > 1

END

Open in new window

0
 

Author Comment

by:amucinobluedot
ID: 41818982
Thx!
0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 41820049
I think all you really have to have is the word "AS".  I don't add an extra "BEGIN" unless I really need it.

CREATE PROCEDURE [dbo].[admin_removeduplicate_links]
AS
;WITH cte_dups AS (
    SELECT
        AttsId,
        ROW_NUMBER() OVER(PARTITION BY CaseId, UserId ORDER BY AttsId DESC) AS row_num
    FROM dbo.Atts
)
DELETE FROM cte_dups
WHERE row_num > 1
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 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