简体   繁体   中英

Comparing 2 different columns of 2 different rows in Oracle 11g

I have computed a table from a query and it looks like this:

UID1 UID2
2    3
2    15
3    2
7    12
12   7
15   2

I only need the unique tuples here. ie Out of the tuples where UID1=2,UID2=3 and UID1=3,UID2=2, I need only 1 tuple in the output.
Tried using join on this table with UID values swapped in the other table, but again the same result appears in the output.
Any suggestions, please?

See this SQL Fiddle that should solve your problem.

It's a self-join on both combinations of uid1 = uid2 and keeps only 1 result based on whichever uid1 is "smaller".

select 
  t.*
from
  test t
  inner join test t2
    on t.uid1 = t2.uid2 
       and t.uid2 = t2.uid1
where
  t.uid1 < t2.uid1;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM