也许对许多人来说是个熟悉的桌子。一张足球联赛表。但是,在这个名单中有一个错误,第4和第5等级完全相等,所以这些球队不应该是第4和第5,而是第4和第4,然后排名应该继续以6。
Ranking | Team | Points | Goals difference | Goals scored | Goals against
1 A 3 4 4 0
2 B 3 3 3 0
3 C 3 1 2 1
4 D 3 1 1 0
5 E 3 1 1 0
6 F 1 0 2 2
7 G 1 0 0 0 我一直试图通过使用公共表表达式并选择ROW_Number来改进生成此表的MS查询,但这并没有给出正确的结果。有人有更好的主意吗?
发布于 2014-11-27 19:23:59
通过使用职级()函数,您可以很容易地做到这一点。
declare @table as table
(
Team varchar(1),
Points int,
GoalsScored int,
GoalsAgainst int
)
insert into @table values ('A', 3, 4, 0),
('B', 3, 3, 0),
('C', 3, 2, 1),
('D', 3, 1, 0),
('E', 3, 1, 0),
('F', 1, 2, 2),
('G', 1, 0, 0)
select RANK() OVER (ORDER BY points desc, GoalsScored - GoalsAgainst desc, GoalsScored desc) AS Rank
,team
,points
,GoalsScored - GoalsAgainst as GoalsDifference
,GoalsScored
,GoalsAgainst
from @table
order by rankhttps://stackoverflow.com/questions/27177343
复制相似问题