我需要找到两个表之间记录的所有唯一对(不是排列)。以下是A
,A_1
的所有排列:
with a1
as(
select
'abc' as a,
1 as dummy
from dual
union all
select
'efg' as a
1 as dummy
from dual)
select
a1_1.a,
a1_2.a
from a1 a1_1
join a1 a1_2 on a1_1.dummy = a1_2.dummy
where a1_1.a != a1_2.a;
输出(带有上面的片段):
+-----+-----+
| A | A_1 |
+-----+-----+
| efg | abc |
| abc | efg |
+-----+-----+
所需的输出:
+-----+-----+
| A | A_1 |
+-----+-----+
| efg | abc |
+-----+-----+
是否有一种获取对(而不是上面的排列)的方法?
我认为您只想要<
:
select a1_1.a, a1_2.a
from a1 a1_1 join
a1 a1_2
on a1_1.dummy = a1_2.dummy and
a1_1.a < a1_2.a;