473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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_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
Jul 19 '05 #1
3 4219
"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
Jul 19 '05 #2
"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)
Jul 19 '05 #3
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

Jul 19 '05 #4

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

Similar topics

4
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 ...
5
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),...
7
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?...
1
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
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
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...
13
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 &...
3
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...
4
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 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...

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.