473,808 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about deleting table duplicates

I was looking for thw SQL to delete dupes from a table, and came across
this. All who saw it agreed in principle, but I can't quite figure out the
logic. If we are deleting all rows whose rowid is greater than the least of
the rowids returned from creating the subset of dupes, couldn't we
inadvertently delete some non-dupes rows that were created after the last
dupe ? I mean, any row created after the last dupe would have a greater
rowid, wouldn't it ?

Here's the SQL:

delete from table_1 a
where a.rowid >
(select min(b.rowid)
from table_1 b
where b.col_dup_value s = a.col_dup_value s)

By the way, should the delete ALL dupes, including the originals ? That is
all rows participating in duplicity, as it is, will be gone.

Thanks,
Scott
Jul 19 '05 #1
3 4257
"ScottH" <fa*********@ne wsgroupsonly.co m> wrote in message news:<pO******* *************@g iganews.com>...
I was looking for thw SQL to delete dupes from a table, and came across
this. All who saw it agreed in principle, but I can't quite figure out the
logic. If we are deleting all rows whose rowid is greater than the least of
the rowids returned from creating the subset of dupes, couldn't we
inadvertently delete some non-dupes rows that were created after the last
dupe ? I mean, any row created after the last dupe would have a greater
rowid, wouldn't it ?

Here's the SQL:

delete from table_1 a
where a.rowid >
(select min(b.rowid)
from table_1 b
where b.col_dup_value s = a.col_dup_value s)

By the way, should the delete ALL dupes, including the originals ? That is
all rows participating in duplicity, as it is, will be gone.

Thanks,
Scott


No, you will delete rows which are identical, save for their rowids.
Rowids aren't stored, they are an internal attribute of a record.
So if you have 2 identical rows, only 1 will be deleted. A *random*
one of course, because rows are inserted randomly.
If you would limit the delete with an extra where clause to one
particular case, you should see easily that only one record is
deleted.
If more rows are deleted, the where clause in the subquery is
incorrect.

Sybrand Bakker
Senior Oracle DBA
Jul 19 '05 #2
"ScottH" <fa*********@ne wsgroupsonly.co m> wrote in message news:<pO******* *************@g iganews.com>...
I was looking for thw SQL to delete dupes from a table, and came across
this. All who saw it agreed in principle, but I can't quite figure out the
logic. If we are deleting all rows whose rowid is greater than the least of
the rowids returned from creating the subset of dupes, couldn't we
inadvertently delete some non-dupes rows that were created after the last
dupe ? I mean, any row created after the last dupe would have a greater
rowid, wouldn't it ?

Here's the SQL:

delete from table_1 a
where a.rowid >
(select min(b.rowid)
from table_1 b
where b.col_dup_value s = a.col_dup_value s)


The condition in the subselect (b.col_dup_valu es = a.col_dup_value s)
links the two instances (a and b) of table_1 in this SQL. The delete
statement, therefore, only affects table_1 with the condition:
(b.col_dup_valu es = a.col_dup_value s).

This won't delete all duplicated rows, as such, but any row that is a
duplicate of a row that already exists - leaving one row where there
were several duplicates. If you wanted to do that the SQL is much
simpler (at least, simpler to follow).

DELETE FROM table_1
WHERE col_dup_values = (SELECT col_dup_values
,COUNT(*)
FROM table_1
WHERE COUNT(*)>1
GROUP BY col_dup_values)
Jul 19 '05 #3
Russ Bagley wrote:
"ScottH" <fa*********@ne wsgroupsonly.co m> wrote in message news:<pO******* *************@g iganews.com>...
I was looking for thw SQL to delete dupes from a table, and came across
this. All who saw it agreed in principle, but I can't quite figure out the
logic. If we are deleting all rows whose rowid is greater than the least of
the rowids returned from creating the subset of dupes, couldn't we
inadvertent ly delete some non-dupes rows that were created after the last
dupe ? I mean, any row created after the last dupe would have a greater
rowid, wouldn't it ?

Here's the SQL:

delete from table_1 a
where a.rowid >
(select min(b.rowid)
from table_1 b
where b.col_dup_value s = a.col_dup_value s)

The condition in the subselect (b.col_dup_valu es = a.col_dup_value s)
links the two instances (a and b) of table_1 in this SQL. The delete
statement, therefore, only affects table_1 with the condition:
(b.col_dup_valu es = a.col_dup_value s).

This won't delete all duplicated rows, as such, but any row that is a
duplicate of a row that already exists - leaving one row where there
were several duplicates. If you wanted to do that the SQL is much
simpler (at least, simpler to follow).

DELETE FROM table_1
WHERE col_dup_values = (SELECT col_dup_values
,COUNT(*)
FROM table_1
WHERE COUNT(*)>1
GROUP BY col_dup_values)


Hmm. This isn't how I saw it.

Pretend data set

rowid col
1 1
2 2
3 1
4 4
5 1
6 6

Ok so the sub query said:
select min(b.rowid)
from table_1 b
where b.col_dup_value s = a.col_dup_value s

That would return 1, right? The min row where a.col = b.col.

So plug that into the original query:

delete from table_1 a
where a.rowid >
(select min(b.rowid)
from table_1 b
where b.col_dup_value s = a.col_dup_value s)

You get:

delete from table_1 a
where a.rowid > (1)

Or am I missing something?

--
Mike Nugent
Programmer/Author/DBA/Admin
In search of employment, email for credentials
ne**@remove-this.illuminatu s.org

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1755
by: Jerry Barnes | last post by:
Suppose that I have a table that contains a lot of records that are identical except for an id field and a date-time-stamp field. For example Id Unit Price DTS 1 A 1.00 Date 1 2 A 1.00 Date 2 3 A 1.00 Date 3 4 B 1.25 Date 4 5 B 1.50 Date 5
5
3395
by: KGuy | last post by:
I have a question on a practice assignment that I can't solve. Can someone help me out? Question: The table Arc(x,y) currently has the following tuples (note there are duplicates): (1,2), (1,2), (2,3), (3,4), (3,4), (4,1), (4,1), (4,1), (4,2). Compute the result of the query: SELECT a1.x, a2.y, COUNT(*)
7
2714
by: Danny J. Lesandrini | last post by:
I know this has been discussed before, as I've seen the Google posts, but they all leave me with an unanswered question: Does my DAO code executed in the front end cause the back end to bloat? (May also substitute UPDATE and/or DELETE queries for DAO code.) I was just brought on to a project with Access 97 where the all data is kept on the server. It doubles in size each day, from 80 mb to 160 mb. The data file contains only tables...
1
1512
by: Brian Keanie | last post by:
Used the "find duplicates" wizard to identify approx 500 duplicates in a single table. How do you delete the duplicates without doing it one at a time?
2
2206
by: Zak McGregor | last post by:
Hi all I have a table, for simplicity's sake containing one field, called unid. for example, select unid, oid from table gives me something like this: unid | oid ---------+---------
3
5213
by: skennd | last post by:
Here's my problem in exact replication: I have used the find duplicate query in Access, and the query determined the following duplicate values by the following query: In (SELECT FROM As Tmp GROUP BY HAVING Count(*)>1 )
13
1917
by: groupy | last post by:
input: 1.5 million records table consisting users with 4 nvchar fields:A,B,C,D the problem: there are many records with dublicates A's or duplicates B's or duplicates A+B's or duplicates B+C+D's & so on. Mathematicly there are 16-1 posibilities for each duplication. aim: find the duplicates & filter them, leave only the unique users which don't have ANY duplication. We can do it by a simple select query that logicly checks the
3
175
by: ScottH | last post by:
I was looking for thw SQL to delete dupes from a table, and came across this. All who saw it agreed in principle, but I can't quite figure out the logic. If we are deleting all rows whose rowid is greater than the least of the rowids returned from creating the subset of dupes, couldn't we inadvertently delete some non-dupes rows that were created after the last dupe ? I mean, any row created after the last dupe would have a greater...
4
2190
by: N2Deep | last post by:
I have a table named SUPPORT DATA, I have a field named Serial Number. In the Serial Number field I have many duplicates, and I only want one of each. Sample serials ABB045000MG, JBX05050016 Until now I have been running an update query which identifies the duplicate serial numbers in the SUPPORT DATA table by: In (SELECT FROM As Tmp GROUP BY HAVING Count(*)>1 ) And Is Not Null When it finds duplicates it updates a field called...
0
9600
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10628
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10373
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10374
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10113
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7651
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6880
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3859
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.