10

How do I return a list of all possible combinations of values from within the same single column of database 'x'? For example I have:

    col 1,
    1
    2
    3
    4

And I would like to return a list of all possible combinations like,

    1,2
    1,3
    1,4
    2,3,
    2,4
    3,4

....

1
  • Are all of your values distinct? Could you ever have a=1,b=1,c=2 and want to return {a=1,b=1, a=1,c=2, b=1,c=2}? Commented Jun 26, 2015 at 10:08

2 Answers 2

18

You've not said which RDBMS you are using or whether you want to limit the combinations to just 2 elements of the set.

Here is an Oracle answer using hierarchical queries:

SQL Fiddle

Oracle 11g R2 Schema Setup:

CREATE TABLE TEST ( COL ) AS
SELECT LEVEL FROM DUAL CONNECT BY LEVEL < 5;

Query 1:

SELECT SUBSTR(SYS_CONNECT_BY_PATH(COL, ','), 2) AS combination
FROM TEST
CONNECT BY PRIOR COL < COL

Results:

| COMBINATION |
|-------------|
|           1 |
|         1,2 |
|       1,2,3 |
|     1,2,3,4 |
|       1,2,4 |
|         1,3 |
|       1,3,4 |
|         1,4 |
|           2 |
|         2,3 |
|       2,3,4 |
|         2,4 |
|           3 |
|         3,4 |
|           4 |

Query 2:

SELECT SUBSTR(SYS_CONNECT_BY_PATH(COL, ','), 2) AS combination
FROM   TEST
WHERE  LEVEL = 2
CONNECT BY PRIOR COL < COL AND LEVEL <= 2

Results:

| COMBINATION |
|-------------|
|         1,2 |
|         1,3 |
|         1,4 |
|         2,3 |
|         2,4 |
|         3,4 |

And an SQL Server version using a recursive CTE:

SQL Fiddle

MS SQL Server 2014 Schema Setup:

CREATE TABLE TEST ( COL INT );

INSERT INTO TEST
          SELECT 1
UNION ALL SELECT 2
UNION ALL SELECT 3
UNION ALL SELECT 4;

Query 1:

WITH cte ( combination, curr ) AS (
  SELECT CAST( t.COL AS VARCHAR(80) ),
         t.COL
  FROM   TEST t
  UNION ALL
  SELECT CAST( c.combination + ',' + CAST( t.col AS VARCHAR(1) ) AS VARCHAR(80) ),
         t.COL
  FROM   TEST t
         INNER JOIN
         cte c
         ON ( c.curr < t.COL )
)
SELECT combination FROM cte

Results:

| combination |
|-------------|
|           1 |
|           2 |
|           3 |
|           4 |
|         3,4 |
|         2,3 |
|         2,4 |
|       2,3,4 |
|         1,2 |
|         1,3 |
|         1,4 |
|       1,3,4 |
|       1,2,3 |
|       1,2,4 |
|     1,2,3,4 |
Sign up to request clarification or add additional context in comments.

Comments

10
select concat(t1.id, ",", t2.id) from t t1 join t t2 on t1.id < t2.id;

An SQL Fiddle example can be found here.

concat(t1.id, ",", t2.id)
-------------------------
1,2
1,3
1,4
2,4
2,3
3,4

2 Comments

Great answer, simple and effective, just a point, based on the criteria the condition of the join can be changed to t1.id <> t2.id
@MohsenSichani - No, <> would allow both 1,2 and 2,1, thus yielding permutations rather than combinations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.