Skip to Main Content
  • Questions
  • Select distinct combinations across rows and columns

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, Patricia.

Asked: August 30, 2011 - 1:10 pm UTC

Last updated: September 01, 2011 - 7:55 am UTC

Version: 11.2

Viewed 10K+ times! This question is

You Asked

I have a table that contains connections between specific areas. However, each connection occurs twice - once on the lead in and once on the lead out. I am trying to find each unique occurrence of a connection across the two columns, regardless of the order the records are stored in the table.

I have tried a number of queries using FIRST_VALUE, but cannot seem to get the result that I am after and my attempts at CONNECT BY PRIOR never return at all.

The data looks like this:
   TAB_NAME  CONX
1    HOME    AB1
2    AB1     HOME
3    AB1     AB2
4    AB2     AB1
5    AB3     AB2
6    AB2     AB3


I am trying to get the unique occurrence of each connection:
HOME - AB1
AB1-AB2
AB2-AB3

create table connections (tab_name varchar2(50), conx varchar2(50));
insert into connections values('HOME', 'AB1');
insert into connections values('AB1', 'HOME');
insert into connections values('AB1', 'AB2');
insert into connections values('AB2', 'AB1');
insert into connections values('AB3', 'AB2');
insert into connections values('AB2','AB3');

Any suggestions that you could provide would be great.

Thanks for your help,

Patricia

and Tom said...

I didn't understand why HOME-AB1 is chosen for one row - but AB3-AB3 is chosen for another. So, assuming you just want links and that the order of them in the row doesn't matter:

ops$tkyte%ORA11GR2> select distinct tab_name, conx
  2    from (
  3  select tab_name , conx
  4    from connections
  5  connect by nocycle prior conx  = tab_name
  6         )
  7   where tab_name < conx
  8  /

TAB_NAME CONX
-------- --------
AB1      AB2
AB1      HOME
AB2      AB3


if the order does matter in the row - you'll need to explicitly tell us what the rules are, what the logic is.

Rating

  (3 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Exactly what I was looking for

Patricia Kneen, August 31, 2011 - 3:05 pm UTC

Thank-you. That will do exactly what I need.

Can be simpler?

Jichao Li, August 31, 2011 - 9:07 pm UTC

Tom, Patricia,

Since each connection always appears twice (lead in/out) in the table, why can't it be written simpler as below?

 SELECT *
   FROM connections
  WHERE tab_name < conx;


TAB_NAME   CONX    
---------- ------- 
AB1        HOME    
AB1        AB2     
AB2        AB3     

Tom Kyte
September 01, 2011 - 7:55 am UTC

You cannot make that assumption unless Patricia says there is a business rule that is enforced and guaranteed to be in place for that data. You cannot observe just a sample of the data and infer business rules from it. We can only work with the specification provided.

another simple answer

SIVAMANU490, April 27, 2015 - 12:04 pm UTC

select distinct greatest(tab_name ,conx),least(tab_name ,conx) from connections