Solved

Delete duplicate rows

Posted on 2008-10-08
2
200 Views
Last Modified: 2010-03-19
Sometimes with this device we have, its possible that duplicate column data will be placed within the SQL Server table.  So, we are trying to create a SPROC in which will remove these DUPLICATES but still obviously preserve the data.   Basicaly would like the query to be based on the ENTRY_DATE column.  

Basically, if there are ANY two entry dates of the same, preserve ONE row and wipe the rest.  There will NEVER be TWO entries within the same second, its just not possible.  

Example would be

          FIRSTNAME           LASTNAME                  ENTRY_DATE
1            Chris                        Jones                     10/7/2008 4:50:00 PM
2            Chris                        Jones                     10/7/2008 4:50:00 PM
3            Chris                        Jones                     10/7/2008 4:50:00 PM

So in the this example, rows 2 and 3 will be deleted, but row 1 will stay because we still want the data.
0
Comment
Question by:chrisryhal
[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 60

Expert Comment

by:chapmandew
ID: 22670742
with mycte(ranking, firstname, lastname, entrydate)
as
(
select ranking = dense_rank() over(partition by firstname, lastname, entry_date order by newid()
, firstname, lastname, entry_date
from yourtable
)

delete from mycte
where ranking > 1
0
 
LVL 13

Accepted Solution

by:
sm394 earned 500 total points
ID: 22672035
SELECT DISTINCT FIRSTNAME, LASTNAME,ENTRY_DATE
 into tmp FROM TableName

delete from TableName

insert into TableName(FIRSTNAME, LASTNAME,ENTRY_DATE)
select FIRSTNAME, LASTNAME,ENTRY_DATE from tmp2

drop table tmp2
0

Featured Post

Independent Software Vendors: 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

I've encountered valid database schemas that do not have a primary key.  For example, I use LogParser from Microsoft to push IIS logs into a SQL database table for processing and analysis.  However, occasionally due to user error or a scheduled task…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

623 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