Solved

Deleting duplicate records in SQL

Posted on 2014-09-10
4
115 Views
Last Modified: 2014-09-22
Experts,

Attached is the documents outlines the sample data and I want to delete the rows that are highlighted in red color. Have been trying with multiple scripts and not successful.

Thanks in advance
Document.docx
0
Comment
Question by:Tpaul_10
  • 2
4 Comments
 
LVL 4

Assisted Solution

by:rlarian
rlarian earned 250 total points
Comment Utility
INSERT INTO tablenew
      SELECT * FROM table tT
      WHERE NOT EXISTS (SELECT * FROM tablenew T
                                    WHERE      T.[ItemNumber] = tT.[ItemNumber] AND
                                                T.[ItemCode] = tT.[ItemCode])

tablenew can be a temp table. then truncate table and insert tablenew back into table.
0
 

Author Comment

by:Tpaul_10
Comment Utility
Didn't get it quite right with the above code rlarian, how about the following query and gets me what I wanted. Can you please review?

Delete ItemDetails where itemId not in (
select min(itemID) from ItemDetails A
group by ItemNumber,ItemCode) 

Open in new window


Thanks
0
 
LVL 4

Expert Comment

by:rlarian
Comment Utility
just tested. that'll work.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
Comment Utility
<air code>  
;with del as (
   SELECT ItemNumber, ItemCode, ItemID
      row_number() OVER (partition by ItemNumber, ItemCode ORDER BY ItemID) as row_number 
   FROM your_table
)
DELETE FROM del WHERE row_number > 1

Open in new window

Nice timing; I just wrote an article titled SQL Server Delete Duplicate Rows Solutions, so if you have some time to kill give this a read and let me know if you like it.  The above is modeled after the 'Delete #1' section.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

After restoring a Microsoft SQL Server database (.bak) from backup or attaching .mdf file, you may run into "Error '15023' User or role already exists in the current database" when you use the "User Mapping" SQL Management Studio functionality to al…
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

762 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

9 Experts available now in Live!

Get 1:1 Help Now