Suppose I have a table with 3 rows: A
, B
, C
DECLARE @t TABLE (col varchar(1))
INSERT @t
SELECT 'A' UNION
SELECT 'B' UNION
SELECT 'C'
I'd like to concatenate the three values an select all possible combinations:
A;B;C;
A;C;B;
B;A;C;
B;C;A;
C;A;B;
C;B;A;
This query gives me the first combination: A;B;C;
SELECT STUFF(
(
SELECT t1.col + ';'
FROM @t AS t1
--CROSS JOIN @t AS t2
FOR XML PATH('')
),1,0,'') AS List
If I uncomment the line with the CROSS JOIN
I get:
A;B;C;A;B;C;A;B;C;
But obvisiously that's not what I'm looking for ;-) First of all, it's all in one row. An second, it's alsways the same combiantion (A;B;C;
, A;B;C;
, A;B;C;
) whereas I'm looking for A;B;C;
, B;C;A;
, C;A;B;