开发者

Oracle result set combinations

I 开发者_StackOverflow中文版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   
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜