Solved

Delete duplicate rows SQL

Posted on 2014-03-21
2
383 Views
Last Modified: 2014-03-21
I have a table with the following columns,
ProductID, BusDate, Code, LastUpdatedDate

I need to delete duplicate rows and just keep a row with the highest timestamp

I tried this query to give me the timestamp that I want to keep and delete the rows that have the same
ProductID, BusDate, Code but a LastUpdatedDate not equal to the max(LastUpdatedDate)

select ProductID, BusDate, Code, MAX(LastUpdatedDate), COUNT(*)
FROM Products
GROUP BY  ProductID, BusDate, Code
HAVING COUNT(*) > 1

3097      2014-03-01 00:00:00.000      COUNTRY            2014-03-11 09:24:06.983            4
3097      2014-03-01 00:00:00.000      INTERNET      2014-03-11 09:24:06.983            4
3099      2014-03-01 00:00:00.000      COMMEQP            2014-03-11 09:24:06.983            4
3099      2014-03-01 00:00:00.000      COUNTRY            2014-03-11 09:24:06.983            4
3115      2014-03-01 00:00:00.000      BANKS            2014-03-11 09:24:06.983            3
3115      2014-03-01 00:00:00.000      COUNTRY            2014-03-11 09:24:06.983            4
0
Comment
Question by:countrymeister
[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
2 Comments
 
LVL 5

Accepted Solution

by:
jayakrishnabh earned 500 total points
ID: 39945256
;WITH CTE AS(
   SELECT ProductID, BusDate, Code, LastUpdateDate,
       RN = ROW_NUMBER()OVER(PARTITION BY ProductID, BusDate, Code ORDER BY LastUpdateDate Desc)
   FROM dbo.Table_2
)
DELETE FROM CTE WHERE RN > 1
0
 
LVL 10

Expert Comment

by:PadawanDBA
ID: 39945257
You could probably use something similar to:

with duplicateRows as
(
	select
		ProductID,
		ROW_NUMBER( ) over( partition by productID, busdate, code order by lastUpdatedDate desc ) as rowNum
	from
		Products
)

delete duplicateRows
	where rowNum > 1;

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how the fundamental information of how to create a table.

719 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