我有一个具有以下结构的表:
Type Value
A abc
A def
A xyz
我想要一个结果集,该结果集将是“值”的所有可能组合,但也应该是不同的。
结果集:
Value
abc def
abc xyz
def xyz
abc def xyz
如果结果集具有“ abc def”,则我不希望显示“ def abc”。即使这些是2个不同的值,这些对我来说都是重复的。我坚持两件事:
任何帮助将不胜感激。
答案 0 :(得分:1)
请尝试以下查询-
WITH CTE (id,Value)
AS
(
SELECT DISTINCT 1,Value
FROM your_table
)
SELECT A.Value + ' '+ B.Value AS Value
FROM CTE A
INNER JOIN CTE B ON A.id = B.id
WHERE (A.Value <> B.Value AND A.Value > B.Value)
UNION ALL
SELECT A.Value + ' '+ B.Value + ' ' + C.Value AS Value
FROM CTE A
INNER JOIN CTE B ON A.id = B.id
INNER JOIN CTE C ON A.id = C.id
WHERE
(
A.Value <>B.Value AND A.Value >B.Value
AND A.Value <> C.Value AND A.Value > C.Value
AND B.Value <> C.Value AND B.Value > C.Value
)