我有一个团队权重列表,基于从每个团队挑选11名球员(基于他们的外场位置)。下面是列表及其代码的屏幕截图:
SELECT TeamID, SUM(PlayerWeighting) as TeamWeight
FROM (
SELECT * FROM(
SELECT pl.*,
ROW_NUMBER() OVER(PARTITION BY pl.TeamID, pl.Position ORDER BY NEWID()) AS Rnk
FROM(
SELECT DISTINCT p.PlayerID, p.Position, p.PlayerWeighting, p.FirstName, p.Surname, t.TeamID, t.TeamAbbreviation, f.WeekNumber
FROM dbo.Fixture f
INNER JOIN dbo.League l ON f.LeagueID = l.LeagueID
INNER JOIN dbo.Team t ON l.LeagueID = t.LeagueID
INNER JOIN dbo.Player p ON t.TeamID = p.TeamID
WHERE f.WeekNumber = 1)
pl) po
WHERE (po.position = 'GK' and po.rnk = 1) OR
(po.position = 'DF' and po.rnk <= 4) OR
(po.position = 'MF' and po.rnk <= 4) OR
(po.position = 'FW' and po.rnk <= 2) ) as T
GROUP BY T.TeamID
我想要做的是将每个团队与他们的反对者逐周联系起来。例如,如果你看下面的夹具清单,我们有4队和60队。所以我想比较4队的teamWeight和60队的TeamWeight,胜利最高的队伍(我知道可以通过CASE完成)何时声明)。
然后想要将第14队与第59队比较,然后将第15队与第43队比较,直到第1周的最后一场比赛,即19队与第35队。
应该是每周一周,因为每天都要运行SQL Agent Job,因此每次代理作业执行时,它都会运行一周的灯具和增量来运行下一周的灯具,以便下次运行。我知道需要为此设置一个周数参数。
我的问题是如何在比较团队权重时让团队相互匹配。对于这个例子,我们可以说获胜团队(更高的团队重量)将三个点插入到固定表中的归位或远点(基于他们是在家还是在路上),失败的团队得到0,如果相等则每个得1分。
谢谢,
更新:
下面显示完全创建灯具列表的代码:
WITH League_Teams AS (
-- Generate a unique-per-league index for each team that is between 0
-- and the (number of teams - 1) and calculate the number of teams
-- if this is an odd number then generate a fake team that's 0.
SELECT TeamID AS id,
LeagueID,
ROW_NUMBER() OVER ( PARTITION BY LeagueID ORDER BY TeamID ) - 1 AS idx,
0 AS is_fake,
COUNT(1) OVER ( PARTITION BY LeagueID ) AS num_teams,
(COUNT(1) OVER ( PARTITION BY LeagueID ) % 2) AS num_fake
FROM Team
UNION ALL
-- Insert a fake team if required
SELECT 0,
LeagueID,
COUNT(1),
1,
COUNT(1),
1
FROM Team
GROUP BY LeagueID
HAVING COUNT(1) % 2 > 0
),
cte AS (
-- Calculate round 1 games
SELECT
idx AS home_idx,
num_teams + num_fake - 1 - idx AS away_idx,
1 AS week_number,
LeagueID AS leagueID,
num_teams AS num_teams,
num_fake AS num_fake
FROM league_teams
WHERE 2 * idx < num_teams
UNION ALL
-- Generate successive rounds with the two cases when the away team has the maximum index or otherwise.
SELECT
CASE away_idx
WHEN num_teams + num_fake - 1 THEN home_idx + 1
ELSE (home_idx + 1) % (num_teams + num_fake -1)
END,
CASE away_idx
WHEN num_teams + num_fake - 1 THEN away_idx
ELSE (away_idx + 1) % (num_teams + num_fake - 1)
END,
week_number + 1,
leagueID,
num_teams,
num_fake
FROM cte
WHERE week_number < (num_teams + num_fake - 1)
)
INSERT INTO dbo.Fixture
-- Join the cte results back to the League_Teams table to convert
-- Indexes used in calculation back to the actual team ids.
SELECT rn,
week_number,
DATEADD(week, week_number - 1, @StartFixtureWeek) AS WeekNumber,
h.id,
NULL,
a.id,
NULL,
NULL,
NULL,
c.leagueid
FROM (
SELECT ROW_NUMBER() OVER (ORDER BY LeagueID, week_number) AS rn,
t.*
FROM (
-- Duplicate the results swapping home and away.
SELECT week_number,
home_idx,
away_idx,
LeagueId
FROM cte
UNION ALL
SELECT week_number + num_teams + num_fake - 1,
away_idx,
home_idx,
LeagueId
FROM cte
) t
) c
INNER JOIN League_Teams h ON ( c.home_idx = h.idx AND c.leagueId = h.LeagueID )
INNER JOIN League_Teams a ON ( c.away_idx = a.idx AND c.leagueId = a.LeagueID )
ORDER BY rn;
更新2:
下面是播放器表,其中包含playerWeightings,当在此问题中首次显示的查询中计算出来时,它构成了团队加权: