Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query to delete duplicate records but leave one

Posted on 2006-07-17
6
Medium Priority
?
1,155 Views
Last Modified: 2008-02-01
I have a table that has a number of duplicate records for each different product, - that I need to delete
At the moment I'm running a "Find Duplicates" query but I don't know how to delete the duplicates leaving one record of each product - I can delete them all, but thats no good to me

SELECT B0101.TRADING_NAME, B0101.AGENT_NO, B0101.RUN_DETAIL_1, B0101.SUPPLY
FROM B0101
WHERE (((B0101.TRADING_NAME) In (SELECT [TRADING_NAME] FROM [B0101] As Tmp GROUP BY [TRADING_NAME],[AGENT_NO] HAVING Count(*)>1  And [AGENT_NO] = [B0101].[AGENT_NO])))
ORDER BY B0101.TRADING_NAME, B0101.AGENT_NO;

I'm about to go to work so I won't reply for about 12 hours
0
Comment
Question by:smidgen
[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
6 Comments
 
LVL 11

Expert Comment

by:pootle_flump
ID: 17120367
Hi

This is a great article with more duplicate deleting options than you could ever need
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=6256

The specific strategy depends on your needs (e.g. is this a 24/7 table with huge volumes of data or something much smaller\ can be inaccessible for a period of time)....

HTH
0
 
LVL 65

Expert Comment

by:rockiroads
ID: 17120486
If your running FInd Duplicates Query, can I assume this is Access?
If so, well in query design of this query, select Query from Main Menu and change to Delete Query
this converts your find duplicates query to delete duplicates

0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 17120609
which database system is this for?
how would you decide which row to keep?

you could always...

select distinct ...
   into a temp table

delete everything in the current table

copy back from the temp table...
0
Learn Veeam advantages over legacy backup

Every day, more and more legacy backup customers switch to Veeam. Technologies designed for the client-server era cannot restore any IT service running in the hybrid cloud within seconds. Learn top Veeam advantages over legacy backup and get Veeam for the price of your renewal

 
LVL 1

Expert Comment

by:no001855
ID: 17122027
Following is a method I have used in oracle :
=================================================================
- Find one row for each id which I want to keep
- Delete the others with the same id

DELETE FROM table t WHERE EXISTS
(SELECT 'OK'
         FROM (SELECT id,rowidForOneOccurence for table WHERE more than one occurence) sq
         WHERE t.id=sq.id and t.ROWID<>sq.rowid)
;

Example for a dummy table t with one idfield id1, which record which are kept is "random" within id
================================================================
DELETE FROM testtrigg t WHERE EXISTS (SELECT 'OK' FROM
(SELECT DISTINCT id1 id1,Max(ROWID) over (PARTITION BY id1) mrowid FROM testtrigg t1 WHERE EXISTS (SELECT id1,Count(*) FROM testtrigg t2 GROUP BY id1 HAVING Count(1) > 1)) sq
WHERE sq.id1=t.id1 AND t.ROWID<>sq.mrowid)
;
0
 
LVL 1

Expert Comment

by:no001855
ID: 17122053
Statement should have been:

DELETE FROM testtrigg t WHERE EXISTS (SELECT 'OK' FROM
    (SELECT DISTINCT id1 id1,Max(ROWID) over (PARTITION BY id1) mrowid FROM testtrigg t1
      WHERE EXISTS (SELECT id,coun(*) FROM testtrigg t2 WHERE t2.id1=t1.id1 GROUP BY id1 HAVING Count(1) > 1)) sq
WHERE sq.id1=t.id1 AND t.ROWID<>sq.mrowid)
;
0
 
LVL 1

Accepted Solution

by:
no001855 earned 750 total points
ID: 17122522
Or rather You may simplify it, since two inner quewries may be combined.

DELETE FROM testtrigg t WHERE EXISTS
  (SELECT 'OK' FROM
    (SELECT id1,Max(ROWID) mrowid FROM testtrigg t1 GROUP BY id1 HAVING Count(1) > 1 ) sq
   WHERE sq.id1=t.id1 AND sq.mrowid<>t.ROWID)
;
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

618 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