Solved

Deleting duplicate records in SQL

Posted on 2014-09-10
4
119 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Concatenating multiple comments into one row 16 61
Calculating Business Hours 19 81
SQL Exceptions 3 36
TSQL DateADD update Question 4 25
Audit has been really one of the more interesting, most useful, yet difficult to maintain topics in the history of SQL Server. In earlier versions of SQL people had very few options for auditing in SQL Server. It typically meant using SQL Trace …
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
I designed this idea while studying technology in the classroom.  This is a semester long project.  Students are asked to take photographs on a specific topic which they find meaningful, it can be a place or situation such as travel or homelessness.…

914 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

14 Experts available now in Live!

Get 1:1 Help Now