Solved

Distinct But Reversed Pairs

Posted on 2009-07-15
4
464 Views
Last Modified: 2012-05-07
I have a table similar to:

colA     colB  colC colD
3          11      ...     ...
54        22
11        3
22        11
22        54

I need only distinct pairs, regardless of the order of the elements of a pair.  That is 3, 11 is a duplicate of 11,3.  I need only one of the two rows. Can be either one.  So the above table should be:

colA     colB
3          11
54        22
22        11

0
Comment
Question by:studioEtc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 24860047
so, you only care about the values in columna and columnb?
0
 
LVL 37

Expert Comment

by:momi_sabag
ID: 24860061
select distinct cola,colb
from yourTable t1
where not exists (select 1 from yourtable t2 where t1.cola = t2.cola and t1.colb = t2.colb and cola>colb)
0
 

Author Comment

by:studioEtc
ID: 24860566
I didn't think I had any problem with other columns but unfortunately I do.  When there are duplicates, I need to retain the row with the highest value in colC.
0
 
LVL 37

Accepted Solution

by:
momi_sabag earned 500 total points
ID: 24873121
try this

select cola, colb, max(colc)
from  (select case when cola <= colb then cola else colb end as cola,
    case when cola <= colb then colb else cola end as colb, colc
    from yourTable ) t1
group by cola,colb
0

Featured Post

Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

718 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question