473,386 Members | 1,715 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,386 software developers and data experts.

delete of mirrored rows

hi.
I've seen ways to delete duplicate rows.
Can someone give me some sql to do this?

I have a table with varchar table_name_start, varchar column_name,
varchar table_name_end;

it has rows like this:
table1 col1 table2
table1 col2 table 3
table2 col1 table1

I'd lke to delete the rows if they exist with the names swapped
around, i.e. like above since the first and third share a column name
and the table_name_start/end matches the others table_name_end/start,
I'd like to delete one and leave the other.

I'm scratching my head trying to figure this out.

thanks
Oct 6 '05 #1
6 1526
What is the key of this table? Please post proper DDL so that we don't
have to guess. I'm going to assume that the combination of all three
columns is unique, in which case try this:

DELETE FROM tbl
WHERE EXISTS
(SELECT *
FROM tbl AS T
WHERE T.table_name_start = tbl.table_name_end
AND T.table_name_end = tbl.table_name_start
AND T.column_name = T.column_name)
AND table_name_start > table_name_end ;

--
David Portas
SQL Server MVP
--

Oct 6 '05 #2
On 6 Oct 2005 01:00:14 -0700, "David Portas"
<RE****************************@acm.org> wrote:
What is the key of this table? Please post proper DDL so that we don't
have to guess. I'm going to assume that the combination of all three
columns is unique, in which case try this:

DELETE FROM tbl
WHERE EXISTS
(SELECT *
FROM tbl AS T
WHERE T.table_name_start = tbl.table_name_end
AND T.table_name_end = tbl.table_name_start
AND T.column_name = T.column_name)
AND table_name_start > table_name_end ;

--
David Portas
SQL Server MVP

David,
Very sorry but I did not realize that would make a difference. (really
I didn't).
There actually was not a primary key (this was a temporary working
table).
I've put a primary key into place now, but I used the script before
that.
It seemed to work fine I've never used the 'AS' before.. I guess I
better study up a bit on it.
Oh.. the ddl was this:
CREATE TABLE [allEdges] (
[table_name_start] [varchar] (255) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL ,
[column_name] [varchar] (255) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL ,
[table_name_end] [varchar] (255) COLLATE
SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO
Oct 6 '05 #3
In this case the key makes a difference to the extent that my DELETE
statement will delete "mirrored" rows but won't delete all duplicates
(all three columns identical). For that you would need a key, a cursor
or an intermediate table. Also, my DELETE won't remove rows with NULLs,
which I can see may be an issue now that you've posted DDL with
nullable columns.

In any case, it makes sense to include keys with your DDL or to state
that your table doesn't have a key. Keys and constraints can make a big
difference to the solution.

--
David Portas
SQL Server MVP
--

Oct 6 '05 #4
On 6 Oct 2005 03:53:10 -0700, "David Portas"
<RE****************************@acm.org> wrote:
In this case the key makes a difference to the extent that my DELETE
statement will delete "mirrored" rows but won't delete all duplicates
(all three columns identical). For that you would need a key, a cursor
or an intermediate table. Also, my DELETE won't remove rows with NULLs,
which I can see may be an issue now that you've posted DDL with
nullable columns.

In any case, it makes sense to include keys with your DDL or to state
that your table doesn't have a key. Keys and constraints can make a big
difference to the solution.

--
David Portas
SQL Server MVP

I understand completely. Thanks for the edifications.
(that's what we're here for, eh?)
Cheers
Oct 6 '05 #5
another way of removing duplicates is to
select distinct * into new table
drop old table
rename the new table

Could be much faster

Oct 6 '05 #6
On 6 Oct 2005 11:32:52 -0700, "Alexander Kuznetsov"
<AK************@hotmail.COM> wrote:
another way of removing duplicates is to
select distinct * into new table
drop old table
rename the new table

Could be much faster

I'm not sure because they are not exact duplicates.. just mirrors
Oct 7 '05 #7

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

Similar topics

0
by: Gordon | last post by:
I have 2 tables t and t1. In this case, t1 is a copy of t. I want to delete rows from t1 based on criteria on the t table and a relationship between t ad t1 (in this case the id column). In the...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
2
by: Bob Ganger | last post by:
Hello, I am working on a project using SQL Server 2000 with a database containing about 10 related tables with a lot of columns containing text. The total current size of the database is about...
11
by: Jonan | last post by:
Hello, For several reasons I want to replace the built-in memory management with some custom built. The mem management itlsef is not subject to my question - it's ok to the point that I have...
16
by: robert | last post by:
been ruminating on the question (mostly in a 390/v7 context) of whether, and if so when, a row update becomes an insert/delete. i assume that there is a threshold on the number of columns of the...
9
by: Dejan | last post by:
Hy, Sorry for my terreble english I have this simple code for deleting rows in mysql table... Everything works fine with it. So, what do i wanna do...: my sql table looks something like...
6
by: polocar | last post by:
Hi, I'm writing a program in Visual C# 2005 Professional Edition. This program connects to a SQL Server 2005 database called "Generations" (in which there is only one table, called...
4
imarkdesigns
by: imarkdesigns | last post by:
Hello everyone.. a newbie programmer here wants to ask why is my codes for deleting rows from my databse won't work.... seems that this codes spread to the net and no one refuse to correct it... ...
3
by: Michel Esber | last post by:
Hello, Environment: DB2 LUW v8 FP15 / Linux I have a table with 50+ Million rows. The table structure is basically (ID - Timestamp). I have two main applications - one inserting rows, and the...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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,...
0
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...

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.