How to generate a permutations or combinations of n rows in m columns?

后端 未结 1 898
醉酒成梦
醉酒成梦 2020-12-29 05:41

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

相关标签:
1条回答
  • 2020-12-29 06:20

    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.

    0 讨论(0)
提交回复
热议问题