Solved

Delete duplicate rows

Posted on 2008-10-08
2
171 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
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

867 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now