Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Finding and Deleting Duplicate Data


Finding and Deleting Duplicate Data

Author
Message
ccubley@queryplan.com
ccubley@queryplan.com
Grasshopper
Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)Grasshopper (19 reputation)

Group: General Forum Members
Points: 19 Visits: 3
Comments posted to this topic are about the content posted at http://www.sqlservercentral.com/columnists/ccubley/findinganddeletingduplicatedata.asp


Chris Cubley
www.queryplan.com
Graham Farrow
Graham Farrow
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 5
Neatly encapsulates several techniques in one place. I will save it in my bits of useful code.


Graham Farrow
DBA
Mastercare Central Division
shepton
shepton
SSC Journeyman
SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)SSC Journeyman (94 reputation)

Group: General Forum Members
Points: 94 Visits: 313
Useful topic to cover but isn't the final solution over-complex?
Couldn't you just use an inequality of PaymentID's on a self-join as follows..?

DELETE Payment
WHERE PaymentID IN
(SELECT P1.PaymentID
FROM Payment P1
INNER JOIN Payment P2
ON P1.CustomerNumber = p2.CustomerNumber
AND
p1.PostedDatetime = p2.PostedDatetime
AND
p1.PaymentAmt = p2.PaymentAmt
AND
p1.PaymentID > p2.PaymentID)

Mark



whunt
whunt
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 1
This is not as complex as using temporary tables and scrubbing processes and then moving to live tables.

That was my first atempt.



cppwiz
cppwiz
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 470
Excellent article ! This is something I plan to use on one of my tables later today.



currym
currym
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 57
Good Article! Thanks.



icata
icata
SSC Veteran
SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)SSC Veteran (238 reputation)

Group: General Forum Members
Points: 238 Visits: 295
I think you can use this approach too

--table structure

CREATE TABLE [a4] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[title] [varchar] (50) ,
[name] [varchar] (50) NULL ,
CONSTRAINT [PK_a4] PRIMARY KEY CLUSTERED
(
[id]
) ON [PRIMARY]
) ON [PRIMARY]
GO



--to have distinct id for the doubles

--the first id's

select distinct a.id, a.title, a.name from a4 a, a4 b where a.title = b.title and a.name = b.name and a.id < b.id




--the last id's

select distinct a.id, a.title, a.name from a4 a, a4 b where a.title = b.title and a.name = b.name and a.id > b.id




--to delete the first id's doubles and get unique rows in the original table


delete from a4 where id in (
select distinct a.id from a4 a, a4 b where a.title = b.title and a.name = b.name and a.id < b.id )

--to delete the last id's doubles and get unique rows in the original table
delete from a4 where id in (
select distinct a.id from a4 a, a4 b where a.title = b.title and a.name = b.name and a.id > b.id )



cppwiz
cppwiz
SSC-Enthusiastic
SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)SSC-Enthusiastic (184 reputation)

Group: General Forum Members
Points: 184 Visits: 470
Just to follow up, I followed all of the sample code and everything worked great the first time. The article was well written and the logic was clear in describing why the steps were necessary. Keep up the good work !!!



Calvin Lawson
Calvin Lawson
Mr or Mrs. 500
Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)

Group: General Forum Members
Points: 582 Visits: 102

This is a pretty good technique, but there are problems with it. Using derived tables in a query can be very powerful, but they can also cause excessive use of tempdb (similar to using # tables). Often, there are other ways of doing the same thing for cheaper.

So, if you don't have to use them, don't. The query below is functionally equivalent to the delete statement in the article, but it costs almost half.


delete p1
From Payment p1
JOIN Payment p2
on p1.CustomerNumber = p2.CustomerNumber and
p1.PostedDateTime = p2.PostedDateTime and
p1.PaymentAmt = p2.PaymentAmt
where p1.PaymentID < p2.PaymentID



Signature is NULL
Calvin Lawson
Calvin Lawson
Mr or Mrs. 500
Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)Mr or Mrs. 500 (582 reputation)

Group: General Forum Members
Points: 582 Visits: 102

Here's the full script:


if object_ID('tempdb..#Payment') is not null drop table #Payment
CREATE TABLE #Payment(
PaymentID int identity Not nUll Primary Key,
CustomerNumber int NOT NULL,
PostedDatetime datetime NOT NULL,
PaymentAmt money NOT NULL,
PaymentMemo varchar(200) NOT NULL,
CCRefNum char(10) NOT NULL
)

Insert #Payment Values (1, '01/01/2004', 1, '', '')
Insert #Payment Values (1, '01/01/2004', 1, '', '')
Insert #Payment Values (2, '01/02/2004', 2, '', '')
Insert #Payment Values (2, '01/02/2004', 2, '', '')
Insert #Payment Values (2, '01/02/2004', 2, '', '')
Insert #Payment Values (3, '01/03/2004', 3, '', '')
Insert #Payment Values (3, '01/03/2004', 3, '', '')
Insert #Payment Values (3, '01/03/2004', 3, '', '')
Insert #Payment Values (3, '01/03/2004', 3, '', '')

delete p1
From #Payment p1
JOIN #Payment p2
on p1.CustomerNumber = p2.CustomerNumber and
p1.PostedDateTime = p2.PostedDateTime and
p1.PaymentAmt = p2.PaymentAmt
where p1.PaymentID < p2.PaymentID

DELETE FROM
p1
FROM
#Payment p1
INNER JOIN
(
SELECT
MAX(PaymentID) AS PaymentID,
CustomerNumber,
PostedDatetime,
PaymentAmt
FROM
#Payment
GROUP BY
CustomerNumber,
PostedDatetime,
PaymentAmt
HAVING
COUNT(*) > 1
  p2
ON(
p1.CustomerNumber = p2.CustomerNumber
AND
p1.PostedDatetime = p2.PostedDatetime
AND
p1.PaymentAmt = p2.PaymentAmt
AND
p1.PaymentID <> p2.PaymentID
 



Signature is NULL
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search