我正在编写一个应用程序,用于计算橄榄球比赛(2015年橄榄球世界杯,但它可以应用于许多其他基于游泳池的比赛)的游泳池阶段的队伍排名。< / p>
在泳池阶段完成时,积分将根据获胜/抽奖/亏损/奖励积分计算,团队按积分排名。 如果两支或更多球队的积分水平相当,那么两支并列球队相互比赛的比赛的胜者将是排名较高的。一旦这个初始排名完成,其他标准(分数差异) ,尝试差异,得分,尝试得分)用于完成排名过程。
我编写了一个执行上述所有操作的查询,除了第一个排名标准之外 - 应根据两支球队所在比赛的获胜者订购两个积分级别。
这是我的数据库架构:
[TEAM] [MATCH]
-TeamId -MatchId
-TeamName -HomeTeamId
-Pool -AwayTeamId
-HomeTeamScore
-HomeTeamTries
-AwayTeamScore
-AwayTeamTries
以下是我用来计算给定池的排名/排名的SQL Server查询:
WITH PoolResults ([MatchId], [TeamId], [Team], [P], [W], [D], [L], [PF], [PA], [PD], [TF], [TA], [TD], [PTS], [BP])
AS (SELECT
M.[MatchId],
M.[HomeTeamId] AS [TeamId],
HT.[TeamName],
1 AS [P],
CASE WHEN M.[HomeTeamScore] > M.[AwayTeamScore] THEN 1 ELSE 0 END AS [W],
CASE WHEN M.[HomeTeamScore] = M.[AwayTeamScore] THEN 1 ELSE 0 END AS [D],
CASE WHEN M.[HomeTeamScore] < M.[AwayTeamScore] THEN 1 ELSE 0 END AS [L],
M.[HomeTeamScore] AS [PF],
M.[AwayTeamScore] AS [PA],
(M.[HomeTeamScore] - M.[AwayTeamScore]) AS [PD],
M.[HomeTeamTries] AS [TF],
M.[AwayTeamTries] AS [TA],
(M.[HomeTeamTries] - M.[AwayTeamTries]) AS [TD],
CASE
WHEN M.[HomeTeamScore] > M.[AwayTeamScore] THEN 4
WHEN M.[HomeTeamScore] = M.[AwayTeamScore] THEN 2
WHEN M.[HomeTeamScore] < M.[AwayTeamScore] THEN 0
END AS [PTS],
CASE
WHEN ((M.[AwayTeamScore] - M.[HomeTeamScore]) BETWEEN 1 AND 7) AND M.[HomeTeamTries] >= 4 THEN 2
WHEN ((M.[AwayTeamScore] - M.[HomeTeamScore]) BETWEEN 1 AND 7) THEN 1
WHEN M.[HomeTeamTries] >= 4 THEN 1
ELSE 0
END AS [BP]
FROM
Match AS M
INNER JOIN Team AS HT
ON M.[HomeTeamId] = HT.[TeamId]
WHERE
M.[HomeTeamScore] IS NOT NULL
AND HT.[Pool] = @Pool
UNION
SELECT
M.[MatchId],
M.[AwayTeamId] AS [TeamId],
AT.[TeamName],
1 AS [P],
CASE WHEN M.[AwayTeamScore] > M.[HomeTeamScore] THEN 1 ELSE 0 END AS [W],
CASE WHEN M.[AwayTeamScore] = M.[HomeTeamScore] THEN 1 ELSE 0 END AS [D],
CASE WHEN M.[AwayTeamScore] < M.[HomeTeamScore] THEN 1 ELSE 0 END AS [L],
M.[AwayTeamScore] AS [PF],
M.[HomeTeamScore] AS [PA],
(M.[AwayTeamScore] - M.[HomeTeamScore]) AS [PD],
M.[AwayTeamTries] AS [TF],
M.[HomeTeamTries] AS [TA],
(M.[AwayTeamTries] - M.[HomeTeamTries]) AS [TD],
CASE
WHEN M.[AwayTeamScore] > M.[HomeTeamScore] THEN 4
WHEN M.[AwayTeamScore] = M.[HomeTeamScore] THEN 2
WHEN M.[AwayTeamScore] < M.[HomeTeamScore] THEN 0
END AS [PTS],
CASE
WHEN ((M.[HomeTeamScore] - M.[AwayTeamScore]) BETWEEN 1 AND 7) AND M.[AwayTeamTries] >= 4 THEN 2
WHEN ((M.[HomeTeamScore] - M.[AwayTeamScore]) BETWEEN 1 AND 7) THEN 1
WHEN M.[AwayTeamTries] >= 4 THEN 1
ELSE 0
END AS [BP]
FROM
Match AS M
INNER JOIN Team AS AT
ON M.[AwayTeamId] = AT.[TeamId]
WHERE
M.[AwayTeamScore] IS NOT NULL
AND AT.[Pool] = @Pool
)
SELECT ROW_NUMBER()
OVER (ORDER BY
SUM([BP] + [PTS]) DESC,
SUM([PD]) DESC,
SUM([TD]) DESC,
SUM([PF]) DESC,
SUM([TF]) DESC) AS [Position],
[TeamId],
[TeamName],
SUM([P]) AS [P],
SUM([W]) AS [W],
SUM([D]) AS [D],
SUM([L]) AS [L],
SUM([PF]) AS [PF],
SUM([PA]) AS [PA],
SUM([PD]) AS [PD],
SUM([TF]) AS [TF],
SUM([TA]) AS [TA],
SUM([BP]) AS [BP],
SUM([BP] + [PTS]) AS [PTS]
FROM
PoolResults
GROUP BY
[TeamId],
[TeamName];
如前所述,除了在两队之间赢得比赛的人之外,这一切都会考虑到两支队伍在同一点上的排名。 是否有人建议如何进行此初始排名?
=====更新为原始帖子=====
澄清 - 在同等积分上可以有超过2支球队,在这种情况下,必须评估2支球队的每个组合以确定排名。 SqlFiddle上的以下示例说明了一个池中有5个团队的场景,其中3个团队的点数相同 - http://sqlfiddle.com/#!6/c0701/3
查询#1显示原始匹配数据(所参加的球队和得分)
查询#2显示未排序的池名称:
Australia (10)
England (10)
Fiji (5)
Uruguay (0)
Wales (10)
查询#3显示了按积分排序的泳池排名:
Australia (10)
England (10)
Wales (10)
Fiji (5)
Uruguay (0)
但是,真正的顺序应该是:
Wales (10)
England (10)
Australia (10)
Fiji (5)
Uruguay (0)
威尔士排名高于英格兰,因为威尔士击败英格兰队,英格兰队排名高于澳大利亚,因为英格兰击败了澳大利亚队
答案 0 :(得分:1)
这很复杂,但我会尝试以下方法。
答案 1 :(得分:1)
这需要sql 2012+使用LEAD()
和LAG()
functions,此时只需要两个团队具有相同的Rank。
<强> SCHEMA 强>
CREATE TABLE Table1
([team] varchar(1), [rank] int);
INSERT INTO Table1
([team], [rank])
VALUES
('A', 1),('B', 1),('C', 2);
CREATE TABLE Table2
([team1] varchar(1), [team2] varchar(1), [win] varchar(1));
INSERT INTO Table2
([team1], [team2], [win])
VALUES
('A', 'B', 'B'), ('C', 'A', 'A'),('C', 'B', 'B');
<强> SQL Fiddle Demo 强>
WITH breakTie AS (
SELECT
[team],
[rank],
LAG([team]) OVER (ORDER BY [rank]) PreviousTeam,
LEAD([team]) OVER (ORDER BY [rank]) NextTeam,
LAG([rank]) OVER (ORDER BY [rank]) PreviousRank,
LEAD([rank]) OVER (ORDER BY [rank]) NextRank
FROM Table1
)
SELECT *, CASE
WHEN B.[rank] = B.[NextRank] and B.[team] = T.[win] THEN 1
WHEN B.[rank] = B.[PreviousRank] and B.[team] = T.[win] THEN 1
ELSE 0
END as breakT
FROM breakTie B
LEFT JOIN Table2 T
ON ( B.team = T.team1 or B.team = T.team2)
AND ( B.NextTeam = T.team1 or B.NextTeam = T.team2)
ORDER BY
[rank],
CASE
WHEN B.[rank] = B.[NextRank] and B.[team] = T.[win] THEN 1
WHEN B.[rank] = B.[PreviousRank] and B.[team] = T.[win] THEN 1
ELSE 0
END
答案 2 :(得分:0)
我想补充一些其他建议。因为您当前的问题存在一些矛盾,并且可能没有适当的 回答 与当前规则。
D, A, B, C
,但最后两个不能强制执行。
inside conference win
的抢七局。因此,您可以计算每个团队在这个团队中获得多少胜利作为一种秩序。 我的猜测是你应该分析这些限制。也许规则的顺序是不同的,你可以将 THIS 问题减少到只有两个团队合作的问题。