查询以获取所有行组合

时间:2019-06-04 06:58:16

标签: tsql

我希望查询从以下数据集中检索所有行组合

这是我的原始数据集。

SId      Sequence                  RId

2976             1                    100
4576             1                    100
19472            1                    100
80591            1                    100
58811            1                    100
70859            1                    100
170941           2                    100
167578           2                    100
131885           2                    100
117608           2                    100
78117            1                    101
69481            1                    101
70987            2                    101
46857            2                    101
28396            2                    101

从这个数据集中,我想要基于RId以及1和2的每个序列的组合的结果。

因此,对于上述针对RId 100的情况,应该有24种组合,例如 以下数据:

RSId    Sid    Sequence       RId
1       2976       1           100
1       170941     2           100
2       2976       1           100
2       167578     2           100
3       2976       1           100
3       131885     2           100

the below is the input table format

CREATE TABLE #temp ( SId INT,Sequence INT,Rid INT)

INSERT into #temp values (2976,1,100)
insert into #temp values (4576,1,100)
insert into #temp values (19472,1,100)
insert into #temp values (80591,1,100)
insert into #temp values (58811,1,100)
insert into #temp values (70859,1,100)
insert into #temp values (170941,2,100)
insert into #temp values (167578,2,100)
insert into #temp values (131885,2,100)
insert into #temp values (117608,2,100)
insert into #temp values (78117,1,101)
insert into #temp values (69481,1,101)
insert into #temp values (70987,2,101)
insert into #temp values (46857,2,101)
insert into #temp values (28396,2,101)

SELECT * FROM #Temp

结果应为以下表格格式:

RSId    Sid     Sequence    RId
1       2976    1           100
1       170941  2           100
2       2976    1           100
2       167578  2           100
3       2976    1           100
3       131885  2           100
4       2976    1           100
4       117608  2           100
5       4576    1           100
5       170941  2           100
6       4576    1           100
6       167578  2           100
7       4576    1           100
7       131885  2           100
8       4576    1           100
8       117608  2           100
9       19472   1           100
9       170941  2           100
10      19472   1           100
10      167578  2           100
11      19472   1           100
11      131885  2           100
12      19472   1           100
12      117608  2           100
13      80591   1           100
13      170941  2           100
14      80591   1           100
14      167578  2           100
15      80591   1           100
15      131885  2           100
16      80591   1           100
16      117608  2           100
17      58811   1           100
17      170941  2           100
18      58811   1           100
18      167578  2           100
19      58811   1           100
19      131885  2           100
20      58811   1           100
20      117608  2           100
21      70859   1           100
21      117608  2           100
22      70859   1           100
22      170941  2           100
23      70859   1           100
23      167578  2           100
24      70859   1           100
24      131885  2           100

1 个答案:

答案 0 :(得分:1)

一种方法是使用公用表表达式,交叉联接和联合。 可能有点麻烦,但是应该有不错的表现:

DECLARE @Rid int = 100;

With cte1 As 
(
    SELECT SID, Sequence, Rid
    FROM #Temp
    WHERE Sequence = 1
    AND Rid = @Rid 
), cte2 AS
(
    SELECT SID, Sequence, Rid
    FROM #Temp
    WHERE Sequence = 2
    AND Rid = @Rid 
), cteCJ AS
(
    SELECT Cte1.Sid As Sid1, Cte1.Sequence As Seq1, Cte1.Rid As Rid,
           Cte2.Sid As Sid2, Cte2.Sequence As Seq2,
           ROW_NUMBER() OVER(ORDER BY Cte1.Sid) As RSId
    FROM Cte1
    CROSS JOIN Cte2   
)

SELECT RSId, Sid1 As Sid, Seq1 As Sequence, Rid
FROM cteCJ

UNION 

SELECT RSId, sid2, Seq2, Rid
FROM cteCJ

ORDER BY RSId, Seq1

结果:

RSId    Sid     Sequence    Rid
1       2976    1           100
1       170941  2           100
2       2976    1           100
2       167578  2           100
3       2976    1           100
3       131885  2           100
4       2976    1           100
4       117608  2           100
5       4576    1           100
5       170941  2           100
6       4576    1           100
6       167578  2           100
7       4576    1           100
7       131885  2           100
8       4576    1           100
8       117608  2           100
9       19472   1           100
9       170941  2           100
10      19472   1           100
10      167578  2           100
11      19472   1           100
11      131885  2           100
12      19472   1           100
12      117608  2           100
13      58811   1           100
13      170941  2           100
14      58811   1           100
14      167578  2           100
15      58811   1           100
15      131885  2           100
16      58811   1           100
16      117608  2           100
17      70859   1           100
17      170941  2           100
18      70859   1           100
18      167578  2           100
19      70859   1           100
19      131885  2           100
20      70859   1           100
20      117608  2           100
21      80591   1           100
21      170941  2           100
22      80591   1           100
22      167578  2           100
23      80591   1           100
23      131885  2           100
24      80591   1           100
24      117608  2           100