Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to delete old duplicate rows?

Author  Topic 

Goalie35
Yak Posting Veteran

81 Posts

Posted - 2009-12-04 : 08:11:45
Lets say I have 3 records:
OrderID| Product Name | Order Date
----------------------------------
11111 | Widget A | 9/17/2008
12323 | Widget A | 8/01/2009
98765 | Widget A | 10/12/2009

Of this list, I want to delete ALL but the most recently ordered record of "Widget A". So in this example, rows 1 & 2 would be deleted, & I would keep the most recent record, row 3.

Does anyone know how I could setup a query to do this?

Thanks.

Sachin.Nand

2937 Posts

Posted - 2009-12-04 : 08:32:22
delete from
(select *,row_number()over(partition by Productname order by orderdate desc)as rowid from producttable)
t where rowid=1

PBUH
Go to Top of Page

kbhere
Yak Posting Veteran

58 Posts

Posted - 2009-12-04 : 08:39:28
DELETE FROM Tablename
WHERE orderdate NOT IN (SELECT MAX(orderdate) FROM Tablename GROUP BY productname)


Balaji.K
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-04 : 08:43:39
quote:
Originally posted by Idera

delete from
(select *,row_number()over(partition by Productname order by orderdate desc)as rowid from producttable)
t where rowid=1

PBUH


It should be rowid>1

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-04 : 08:44:06
Also see what you can do with row_number() function
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/08/27/multipurpose-row-number-function.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2009-12-04 : 09:11:00
quote:
Originally posted by madhivanan

quote:
Originally posted by Idera

delete from
(select *,row_number()over(partition by Productname order by orderdate desc)as rowid from producttable)
t where rowid=1

PBUH


It should be rowid>1

Madhivanan

Failing to plan is Planning to fail



Yeah it shud be that way.Thanks for the catch.
Hope the OP did not try my query on his DB or else it wd hd messed up things for him.

PBUH
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-12-04 : 09:20:37
quote:
Originally posted by Idera

quote:
Originally posted by madhivanan

quote:
Originally posted by Idera

delete from
(select *,row_number()over(partition by Productname order by orderdate desc)as rowid from producttable)
t where rowid=1

PBUH


It should be rowid>1

Madhivanan

Failing to plan is Planning to fail



Yeah it shud be that way.Thanks for the catch.
Hope the OP did not try my query on his DB or else it wd hd messed up things for him.

PBUH


Thats why ALL DELETEs and UPDATEs should be tested in TEST server or with Transaction

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -