oracle sql 创建相同的组合 table 而不重复

oracle sql creating combinations same table without repeat

我该如何解决这个问题? 我需要消除 table 本身的笛卡尔积的重复项。 我想到了使用 "connect by"。谢谢

create table foo (
num number(2)
);

insert into foo values (1);
insert into foo values (2);
insert into foo values (3);

select a.num,b.num
from foo a, foo b;

NUM NUM
--- ---
 1   1 
 1   2 
 1   3 
 2   1 * duplicated
 2   2 
 2   3 
 3   1 * duplicated
 3   2 * duplicated
 3   3 
select a.num,b.num
from foo a, foo b
where a.num = b.num

你可以试试这个:

select a.num,b.num
  from foo a cross join foo b
 where a.num <= b.num

SQL Fiddle

Oracle 11g R2 架构设置:

create table foo ( num ) AS
SELECT LEVEL FROM DUAL CONNECT BY LEVEL <= 3;

查询 1:

select a.num,b.num
from foo a CROSS JOIN foo b
WHERE a.num <= b.num

Results:

| NUM | NUM |
|-----|-----|
|   1 |   1 |
|   1 |   2 |
|   1 |   3 |
|   2 |   2 |
|   2 |   3 |
|   3 |   3 |

不需要交叉联接,因为您要在其中匹配 table 第二个实例中与第一个实例中的编号相同或更大的行的联接table 的实例。这就是你如何防止 "duplicate" 条目:

with foo as (SELECT LEVEL num FROM DUAL CONNECT BY LEVEL <= 3)
select f1.num f1_num,
       f2.num f2_num
from   foo f1
       join foo f2 on (f1.num <= f2.num);

    F1_NUM     F2_NUM
---------- ----------
         1          1
         1          2
         1          3
         2          2
         2          3
         3          3