简体   繁体   English

比较Oracle 11g中2个不同行的2个不同列

[英]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. 即,在UID1 = 2,UID2 = 3且UID1 = 3,UID2 = 2的元组中,输出中只需要1个元组。
Tried using join on this table with UID values swapped in the other table, but again the same result appears in the output. 尝试使用此表上的连接,在另一个表中交换UID值,但输出中再次出现相同的结果。
Any suggestions, please? 有什么建议吗?

See this SQL Fiddle that should solve your problem. 请参阅此SQL小提琴 ,它可以解决您的问题。

It's a self-join on both combinations of uid1 = uid2 and keeps only 1 result based on whichever uid1 is "smaller". 它是uid1 = uid2的两种组合的自连接,并且只根据uid1“较小”的值保留1个结果。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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