|
|
|
Grasshopper
      
Group: General Forum Members
Last Login: Wednesday, February 27, 2008 9:40 PM
Points: 19,
Visits: 3
|
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Tuesday, July 15, 2008 12:43 AM
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
|
|
|
|
|
SSC Journeyman
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 4:34 AM
Points: 92,
Visits: 282
|
|
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
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Tuesday, February 24, 2004 7:31 AM
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.
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 8:57 PM
Points: 163,
Visits: 355
|
|
Excellent article ! This is something I plan to use on one of my tables later today.
|
|
|
|
|
SSC Rookie
      
Group: General Forum Members
Last Login: Thursday, August 30, 2012 12:27 PM
Points: 49,
Visits: 38
|
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 8:07 AM
Points: 229,
Visits: 257
|
|
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 )
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Thursday, May 16, 2013 8:57 PM
Points: 163,
Visits: 355
|
|
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 !!!
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, June 21, 2011 10:03 AM
Points: 577,
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
|
|
|
|
|
Mr or Mrs. 500
      
Group: General Forum Members
Last Login: Tuesday, June 21, 2011 10:03 AM
Points: 577,
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
|
|
|
|