0

Given the following

CREATE TABLE #Test 
(
ItemName VARCHAR(2)
);

INSERT INTO #Test (ItemName)
VALUES  ('x1'),
    ('x2'),
    ('x3'),
    ('x4');

How can I get every possible combination regardless of the order of the values? For example x1, x2 is the same as x2, x1 so I don't need both. Also, x1, x2, x3 would be the same as x3, x1, x2 or x2, x1, x3

The output would be:

x1, x2
x1, x3
x1, x4
x2, x3
x2, x4
x3, x4
x1, x2, x3
x1, x2, x4
x1, x3, x4
x2, x3, x4
x1, x2, x3, x4

My values would probably go to x16 so I'm trying to keep this simple for the sake of the example.

I've read a bunch of similar questions, but they all stop with combining just 2 values.

Thanks!

Jeff Brady
  • 1,454
  • 7
  • 35
  • 56

0 Answers0