我的桌子如下。实际上球员按球队1和2的总积分排名。
teamNO playername
===================
1 Joe
1 Jack
1 Micheal
1 Ken
1 Fendy
2 Helen
2 Mike
2 Chris
2 George
2 Dan
我想写下我的桌子,但我只得到一排。我认为由于teamno只有1和2.有没有办法实现这一目标?提前致谢
Rank Team1 Team2
================================
1 Joe Helen
2 Jack Mike
3 Micheal Chris
4 Ken George
5 Fendy Dan
答案 0 :(得分:0)
;WITH cte AS(
SELECT * FROM (VALUES
(1, 'Joe'),
(1, 'Jack'),
(1, 'Micheal'),
(1, 'Ken'),
(1, 'Fendy'),
(2, 'Helen'),
(2, 'Mike '),
(2, 'Chris'),
(2, 'George'),
(2, 'Dan')
) as t(teamNO, playername)),
final AS (
SELECT ROW_NUMBER() OVER (PARTITION BY TeamNo ORDER BY TeamNo) AS [Rank],*
FROM cte)
SELECT f1.[Rank], f1.playername as Team1, f2.playername as Team2
FROM final f1
LEFT JOIN final f2 ON f1.[Rank] = f2.[Rank] and f1.TeamNo != f2.TeamNo
WHERE f1.teamNO = 1
结果:
Rank Team1 Team2
-------------------- ------- -------
1 Joe Helen
2 Jack Mike
3 Micheal Chris
4 Ken George
5 Fendy Dan
(5 row(s) affected)