Solved

Deleting duplicate records in SQL

Posted on 2014-09-10
4
128 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
[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
4 Comments
 
LVL 4

Assisted Solution

by:rlarian
rlarian earned 250 total points
ID: 40315943
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
ID: 40316007
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
ID: 40316054
just tested. that'll work.
0
 
LVL 65

Accepted Solution

by:
Jim Horn earned 250 total points
ID: 40316127
<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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Format Date fields 11 64
Disable TLS1.0 on Win 2012 server 7 117
How come this XML node is not read? 3 53
store vs query adhoc - no show rows 4 37
In this article I will describe the Copy Database Wizard 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.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

752 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