Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting duplicate records in SQL

Posted on 2014-09-10
4
Medium Priority
?
131 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 1000 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 66

Accepted Solution

by:
Jim Horn earned 1000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

722 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