Solved

Distinct But Reversed Pairs

Posted on 2009-07-15
4
426 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
  • 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now