2015-08-28 4 views
0

J'ai une table et je veux produire un cartésien de ces données avec la même table elle-même.Produit cartésien à la même table en oracle sans duplication

TABLE: TEMP_TEST 

     FROM_COL 
     -------------- 
      A  
      B 
      C 

Si j'écris la requête ci-dessous pour cartésien puis-je obtenir la sortie

 SELECT A.FROM_COL FROM_COL1, 
    B.FROM_COL FROM_COL2 
FROM TEMP_TEST A, 
    TEMP_TEST B 
WHERE A.FROM_COL!=B.FROM_COL ; 

sortie

FROM_COL1 FROM_COL2 
    A    B 
    A    C 
    B    A 
    B    C 
    C    A 
    C    B 

Mais si A à B est présent je ne veux pas B à A Comment puis-je écrire une requête pour cela?

J'ai besoin de la sortie ci-dessous

FROM_COL1 FROM_COL2 
     A    B 
     A    C 
     B    C 

Répondre

2

Vous étiez très proche. Il suffit de changer votre != en <:

with temp_test as (select 'A' from_col from dual union all 
        select 'B' from_col from dual union all 
        select 'C' from_col from dual) 
select a.from_col from_col1, 
     b.from_col from_col2 
from temp_test a, 
     temp_test b 
where a.from_col < b.from_col; 

FROM_COL1 FROM_COL2 
--------- --------- 
A   B   
A   C   
B   C   

Il serait préférable (plus déplu/industrie.) Si vous Réécriture de la requête en utilisant la syntaxe ANSI joindre, si:

select a.from_col from_col1, 
     b.from_col from_col2 
from temp_test a 
     inner join temp_test b on (a.from_col < b.from_col); 
+0

Merci ... Il résolu mon problème ... – user5073139