Removing a transposed record

I have a table with two fields in it, Contact and RelatedContact.  
The first two records look like this:

ID  Contact   RelatedContact
1    1000        7000
2    7000        1000

I need to have just one record in this table, doesn't matter which one.  I can't have two records
in this table that are the reverse of each other.  How can I eliminate one of them?
nolasaintsgalAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin MillerCTOCommented:
Typically you would do something like this, shown below, where you insert the table_name and column_name. A row deletion example follows.

For a column:
ALTER TABLE "table_name" DROP "column_name";

Open in new window


For a row:

DELETE FROM "table_name"  WHERE "column_name"  = 'SOME_MATCH_STRING_OR_VALUE';

Open in new window


Let me know if you have other questions
0
nolasaintsgalAuthor Commented:
Sorry I don't believe I was clear enough.  This table will have multiple records where they will appear transposed:

ID  Contact  RelatedContact
1   1000        1007
2   1007        1000
3   2000        1004
4   1004        2000

So I need something where it will look at first record and see that there is a second record with the values transposed and delete one of those transposed records.  So in this instance, records 2 and 4 could be deleted because are equal to records 1 and 3, respectively, when transposed.
0
Scott PletcherSenior DBACommented:
Do two things:
1) Delete all the dups
2) Add a constraint that the Contact value must be less than the RelatedContact value, and the combination of the two must be unique.  Then you can never get dups values like that again.

No data to test with, so this is "air' code in a sense:

1)
DELETE FROM tn
FROM dbo.table_name tn
WHERE Contact > RelatedContact AND
    EXISTS (
        SELECT 1
        FROM dbo.table_name tn2
        WHERE
            tn2.Contact < tn2.RelatedContact AND
            tn2.Contact = tn.RelatedContact AND
            tn2.RelatedContact = tn.Contact )

2)
ALTER TABLE dbo.table_name
ADD CONSTRAINT table_name__CK_Contacts CHECK(Contact < RelatedContact);
ALTER TABLE dbo.table_name
ADD CONSTRAINT table_name__UQ_Contacts UNIQUE ( Contact, RelatedContact );
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
SQL

From novice to tech pro — start learning today.