包含一列的表格和值在下面给出
Table
------------
Ind
Pak
SL
Output
------------
Ind VS Pak
Ind VS SL
SL VS Pak
输出应该是每行作为上面提到的单个列中的字符串。
答案 0 :(得分:2)
您可以使用CTE获得所需的结果:
DECLARE @Data TABLE (Team VARCHAR(100))
INSERT @Data VALUES ('Ind'), ('Pak'), ('SL')
;WITH CTE AS (
SELECT
Team,
ROW_NUMBER() OVER (ORDER BY Team) AS RowNum
FROM @Data
)
SELECT
R1.Team + ' VS ' + R2.Team AS [Output]
FROM CTE R1
INNER JOIN CTE R2
ON R1.RowNum < R2.RowNum
输出:
Output
---------------
Ind VS Pak
Ind VS SL
Pak VS SL
您不一定需要CTE,但我更愿意避免加入VARCHAR值。
SELECT
R1.Team + ' VS ' + R2.Team AS [Output]
FROM @Data R1
INNER JOIN @Data R2
ON R1.Team < R2.Team