简体   繁体   中英

Oracle result set combinations

I have a query which returns the result set as follows:

  Col1
   A
   B
   C
   D

Is it possible to get the following result set? That is associating a value to remaining 3 row values?

col1        col2
A            B
A            C
A            D
B            A
B            C
B            D
C            A
C            B
C            D
D            A
D            B
D            C

I am using Oracle 10g

You can get this with a self join as follows:

SELECT a.col1, b.col1 as col2
  FROM <YOUR_TABLE> a,
       <YOUR_TABLE> b
  WHERE a.col1 <> b.col1  

Working example:

WITH DAT AS
(
  SELECT 'A' NAME FROM DUAL
  UNION
  SELECT 'B' NAME FROM DUAL
  UNION
  SELECT 'C' NAME FROM DUAL
  UNION
  SELECT 'D' NAME FROM DUAL

)
SELECT *
  FROM DAT A, DAT B
 WHERE a.Name <> b.Name   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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