按数字顺序排列的多个值组合

Multiple Combinations of Values in Numerical Order

我已经搜索并尝试自己解决这个难题(我已经接近了,但我没有运气)。我有大量 table 个值(由值集组成),可以有多个组合,但这些组合必须按 ID 顺序返回。

我无法在 SQL 中使用它。

示例集:

(抱歉,我无法 post 可以更好地解释它的图像,所以我会保持简单。)

Table[(ID, Value) {(1,A),(1,B),(1,C),(2,D),(3,F),(3,G), (4,J), (5,S),(5,T),(5,U))}

RESULTS
ID   VALUE

1   A
2   F
3   G
4   J
5   S

1   A
2   F
3   G
4   J
5   T

1   A
2   F
3   G
4   J
5   U

1   A
2   F
3   H
4   J
5   S

1   A
2   F
3   H
4   J
5   T

1   A
2   F
3   H
4   J
5   U

1   B
2   F
3   G
4   J
5   S

1   B
2   F
3   G
4   J
5   T

1   B
2   F
3   G
4   J
5   U

1   B
2   F
3   H
4   J
5   S

1   B
2   F
3   H
4   J
5   T

1   B
2   F
3   H
4   J
5   U

1   C
2   F
3   G
4   J
5   S

1   C
2   F
3   G
4   J
5   T

1   C
2   F
3   G
4   J
5   U

1   C
2   F
3   H
4   J
5   S

1   C
2   F
3   H
4   J
5   T

1   C
2   F
3   H
4   J
5   U

您可以使用 SQL windows 函数来实现。

;WITH CTE AS
    (
    SELECT Id, 
    Value, 
    ROW_NUMBER() OVER (PARTITION BY ID ORDER BY ID) RN 
    FROM Tbl
    )

SELECT * FROM CTE ORDER BY RN, ID, VALUE

Fiddle

我想这个问题之前已经在这里回答过:

How to generate all possible data combinations in SQL?

不同之处在于他们基本上删除了 ID 列,不过应该很容易将其拉过。

这是动态 SQL 中的问题,没有任何游标或循环。

IF OBJECT_ID('yourTable') IS NOT NULL
    DROP TABLE yourTable;

CREATE TABLE yourTable (ID INT, Value CHAR(1));

INSERT INTO yourTable
VALUES  (1,'A'),(1,'B'),(1,'C'),
        (2,'D'),
        (3,'F'),(3,'G'),
        (4,'J'),
        (5,'S'),(5,'T'),(5,'U');

DECLARE @row_number_cols    VARCHAR(MAX),
        @Aliased_Cols       VARCHAR(MAX),
        @Cross_Joins        VARCHAR(MAX),
        @Unpivot            VARCHAR(MAX);


SELECT  @row_number_cols =  COALESCE(@row_number_cols + ',','') + col,
        @Aliased_Cols =     COALESCE(@Aliased_Cols + ',','') + CONCAT(col,' AS col',ID),
        @Cross_Joins =      COALESCE(@Cross_Joins,'') + CASE
                                                            WHEN ID = 1 THEN CONCAT(' FROM (SELECT * FROM yourTable WHERE ID = 1) AS ID',ID)
                                                            ELSE CONCAT(' CROSS JOIN (SELECT * FROM yourTable WHERE ID = ',ID,') AS ID',ID)
                                                        END,
        @Unpivot = COALESCE(@Unpivot + ',','') + CONCAT('col',ID)
FROM yourTable A
CROSS APPLY (SELECT CONCAT('ID',ID,'.Value')) CA(col) --Just so I can reuse "col" in my code
GROUP BY A.ID,CA.col

SELECT @row_number_cols,@Aliased_Cols,@Cross_Joins,@Unpivot
SELECT
'WITH CTE_crossJoins
AS
(
SELECT  ROW_NUMBER() OVER (ORDER BY ' + @row_number_cols + ') group_num,' + @Aliased_Cols +
@Cross_Joins + '
)

SELECT  group_num,
        val
FROM CTE_crossJoins
UNPIVOT
(
    val for col IN (' + @Unpivot + ')
) unpvt
ORDER BY 1,2'

结果:

group_num            val
-------------------- ----
1                    A
1                    D
1                    F
1                    J
1                    S
2                    A
2                    D
2                    G
2                    J
2                    S
3                    A
3                    D
3                    G
3                    J
3                    T
4                    A
4                    D
4                    F
4                    J
4                    T
5                    A
5                    D
5                    F
5                    J
5                    U
6                    A
6                    D
6                    G
6                    J
6                    U
7                    B
7                    D
7                    G
7                    J
7                    S
8                    B
8                    D
8                    F
8                    J
8                    S
9                    B
9                    D
9                    F
9                    J
9                    T
10                   B
10                   D
10                   G
10                   J
10                   T
11                   B
11                   D
11                   G
11                   J
11                   U
12                   B
12                   D
12                   F
12                   J
12                   U
13                   C
13                   D
13                   F
13                   J
13                   S
14                   C
14                   D
14                   G
14                   J
14                   S
15                   C
15                   D
15                   G
15                   J
15                   T
16                   C
16                   D
16                   F
16                   J
16                   T
17                   C
17                   D
17                   F
17                   J
17                   U
18                   C
18                   D
18                   G
18                   J
18                   U