需要帮助才能编写查询
我有一列有一列,有值A,B,C& D.
如何编写查询以获得以下结果: AB,AC,AD,BA,BC,BD,......
对此有任何身体帮助。提前致谢
答案 0 :(得分:2)
尝试:
with x as (
select 'A' as col from dual
union all
select 'B' as col from dual
union all
select 'C' as col from dual
union all
select 'D' as col from dual
)
select *
from x join x x2 on x.col != x2.col
输出:
COL COL_1
"A" "B"
"A" "C"
"A" "D"
"B" "A"
"B" "C"
"B" "D"
"C" "A"
"C" "B"
"C" "D"
"D" "A"
"D" "B"
"D" "C"
答案 1 :(得分:1)
这个SQL可以解决这个问题:
SELECT T1.COL || T2.COL
FROM MY_TABLE T1, MY_TABLE T2
WHERE T1.COL != T2.COL;
此处,MY_TABLE
是包含COL
列的表格。
答案 2 :(得分:0)
很酷的答案。这是我的版本,两个答案的组合:
WITH x AS (
SELECT 'A' AS col FROM dual
UNION ALL
SELECT 'B' AS col FROM dual
UNION ALL
SELECT 'C' AS col FROM dual
UNION ALL
SELECT 'D' AS col FROM dual
)
SELECT x1.col || x2.col AS xx
FROM x x1 JOIN x x2 ON x1.col <> x2.col