在表中查找重复的对称行

C D*_*pak 3 sql oracle

我有一个包含数据的表

col1  col2  
a      b  
b      a  
c      d
d      c
a      d 
a      c
Run Code Online (Sandbox Code Playgroud)

对我来说,第1行和第2行是重复的,因为a, b&b, a是相同的.第3行和第4行也是如此.

我需要一个SQL(不是PL/SQL)查询,它将输出作为

col1   col2
a       b
c       d
a       d
a       c
Run Code Online (Sandbox Code Playgroud)

a_h*_*ame 8

select distinct least(col1, col2), greatest(col1, col2)
from your_table
Run Code Online (Sandbox Code Playgroud)

编辑:对于使用DBMS是不支持的标准SQL功能的leastgreatest这可以使用一个CASE表达式来模拟:

select distinct 
       case 
         when col1 < col2 then col1
         else col2
       end as least_col, 
       case 
         when col1 > col2 then col1
         else col2
        end as greatest_col 
from your_table
Run Code Online (Sandbox Code Playgroud)