如何获得SQL格式的输出

时间:2017-01-22 19:29:22

标签: sql sql-server database postgresql sql-server-2008

我有这样的SQL表,

Column_TEAM
AUS
USA
IND
RUS

预期输出是(一支球队只能与另一支球队比赛一次)

AUS vs USA
AUS vs IND
AUS vs RUS
USA vs IND
USA vs RUS
IND vs RUS

我尝试使用Rank()函数,没有成功。 可以请帮助我在SQL Server或PostgreSQL

2 个答案:

答案 0 :(得分:3)

您需要按如下方式自行加入您的表格:

-- SQL Server concatenation syntax
SELECT a.Column_TEAM + ' vs ' + b.Column_TEAM
FROM my_table a
JOIN my_table b ON a.Column_TEAM < b.Column_TEAM

-- PostgreSQL concatenation syntax
SELECT a.Column_TEAM || ' vs ' || b.Column_TEAM
FROM my_table a
JOIN my_table b ON a.Column_TEAM < b.Column_TEAM

答案 1 :(得分:1)

您也可以使用简单的内部联接来完成。你去吧......

SELECT T2.CTRY2,T1.CTRY1
FROM 
(SELECT CTRY AS CTRY1,ROW_NUMBER() OVER(ORDER BY CTRY) AS R1 FROM (VALUES ('AUS'),('USA'),('IND'),('RUS') ) CT (CTRY)) T1

JOIN 

(SELECT CTRY AS CTRY2,ROW_NUMBER() OVER(ORDER BY CTRY) AS R2 FROM (VALUES ('AUS'),('USA'),('IND'),('RUS') ) CT (CTRY)) T2

ON T1.R1 > T2.R2
ORDER BY T1.CTRY1 ASC