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_values = a.col_dup_values)
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 3 4225
"ScottH" <fa*********@newsgroupsonly.com> wrote in message news:<pO********************@giganews.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_values = a.col_dup_values)
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
"ScottH" <fa*********@newsgroupsonly.com> wrote in message news:<pO********************@giganews.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_values = a.col_dup_values)
The condition in the subselect (b.col_dup_values = a.col_dup_values)
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_values = a.col_dup_values).
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)
Russ Bagley wrote: "ScottH" <fa*********@newsgroupsonly.com> wrote in message news:<pO********************@giganews.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_values = a.col_dup_values)
The condition in the subselect (b.col_dup_values = a.col_dup_values) 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_values = a.col_dup_values).
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_values = a.col_dup_values
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_values = a.col_dup_values)
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.illuminatus.org This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 ...
|
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),...
|
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?...
|
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?
|
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
---------+---------
| |
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...
|
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 &...
|
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...
|
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
...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The...
|
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 ...
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |