如何在m列中生成n行的排列或组合?

时间:2021-06-22 04:39:19

Does anyone know how to create a table with m columns from table of n rows where the values in columns of each row represent a different combination or permutation of values from the original table?

有没有人知道如何从n行表中创建一个包含m列的表,其中每行的列中的值表示原始表中值的不同组合或排列?

For example the original table has 1 column (number_value) with 3 (n=3) rows:

例如,原始表有1列(number_value),其中3(n = 3)行:

1
2
3

The table which contains combinations (the order doesn't matter) of two values (m = 2) would be the following:

包含两个值(m = 2)的组合(顺序无关紧要)的表格如下:

number1, number2
1,2
1,3
2,3

and the table of permutations would be the following:

排列表如下:

number1, number2
1, 2
2, 1
1, 3
3, 1
2, 3
3, 2

The order of rows doesn't matter.

行的顺序无关紧要。

Thank you in advance!

先谢谢你!

1 个解决方案

#1


29  

Combinations:

组合:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x < T2.x

Permutations:

排列:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x != T2.x

I am assuming that the values in the original table are unique.

我假设原始表中的值是唯一的。

To generalize for larger values of m you need to add more joins.

要概括m的较大值,您需要添加更多连接。

#1


29  

Combinations:

组合:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x < T2.x

Permutations:

排列:

SELECT T1.x, T2.x
FROM your_table T1
JOIN your_table T2
ON T1.x != T2.x

I am assuming that the values in the original table are unique.

我假设原始表中的值是唯一的。

To generalize for larger values of m you need to add more joins.

要概括m的较大值,您需要添加更多连接。