单个SELECT语句,用于检索1列的所有可能组合值Oracle

时间:2017-10-17 17:27:46

标签: sql oracle oracle11g

说我有一个专栏:

COLUMN
Bread
Butter
Toast
Jam

我需要一个有效的选择查询,它将返回:

COLUMN
Bread Butter
Bread Toast
Bread Jam
Butter Bread
Butter Toast
Butter Jam
Toast Bread
Toast Butter
Toast Jam
Jam Bread
Jam Butter
Jam Toast

3 个答案:

答案 0 :(得分:3)

以下查询应该执行您想要的操作,它将表连接到自身并删除与项匹配的行。

SELECT 
      T1.COLUMN || ' ' || T2.COLUMN
FROM
      TABLE T1
JOIN
       TABLE T2 ON T2.COLUMN <> T1.COLUMN

答案 1 :(得分:1)

select c1 || ' ' || c2
from 
( select column as c1 from table ),
( select column as c2 from table )
where c1 <> c2 -- this avoids the commented 'jam jam' :)

答案 2 :(得分:0)

这不只是一个带有简单过滤器的cross join吗?

with [Data] as (
         select *
         from (values
             (N'Bread')
           , (N'Butter')
           , (N'Toast')
           , (N'Jam')
         ) as a ([Value])
     )
select a.[Value] + N' ' + b.[Value] as [Value]
from [Data] as a
cross join [Data] as b
where a.[Value] <> b.[Value];