我有这个示例表:
CREATE TABLE Balls
(
BallId int primary key identity(1,1) NOT NULL,
Color char(1) NOT NULL,
Number int NOT NULL
)
我有不同颜色的球,每个球上都有一个数字。表中数据示例:
BallId Color Number
4 G 1
9 G 2
1 R 1
2 R 2
3 R 3
5 W 1
6 W 2
7 W 3
8 W 4
我需要所有不同球的所有排列(在本例中为 G-R 和 W)以及数字。 例如:
G1 R1 W1
G1 R1 W2
G1 R1 W3
G1 R1 W4
G1 R2 W1
...
G2 R3 W4
“G1 R1 W1”等于“R1 G1 W1”(顺序无关紧要)。 我需要 tsql 脚本来生成这个排列。提前致谢!
最佳答案
除非您采取措施限制结果,否则连接表将产生排列。
select g.color, g.number, r.color, r.number, w.color, w.number
from Balls g, Balls r, Balls w
where g.color = 'G'
and r.color = 'R'
and w.color = 'W'
关于sql-server - 生成 tsql 色号排列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20975366/