smidgen
asked on
Query to delete duplicate records but leave one
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
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
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
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
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...
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...
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)
;
==========================
- 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)
;
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)
;
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)
;
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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