所以,我有8个分区,每个分区有5支球队,将进行为期六周的赛季。每个团队将在他们的部门(4周)中扮演每个团队,然后与其他部门一起玩两场比赛。每周球队将以两个中的最佳状态相互比赛。因此,在下面的示例中,第1组与第2组将进行两次,一次以第1组为主队,第二次以第2队为主队。共240场比赛。
这样的事情:
第1周
第2周
第3周
第4周
第5周
第6周
我有一个包含所有teamtable
s(PK)和teamkey
的团队表(division
)。
我的匹配表(matchtable
)包含以下列:MatchKey
,MatchDate
,HomeTeamKey
,AwayTeamKey
TeamKey
的{p> teamtable
可以是hometeamkey
或awayteamkey
。
我们已经使用了相当多的代码迭代,但这是我们到目前为止所做的。
TL; DR - 8个师,每队5个队,6个星期。每周你都会与不同的团队一起打两场比赛。一个团队可以扮演其所在部门的每个人。一支球队不能多次参加同一支球队。 计划可以用SQL编写,还是我尝试过于复杂的东西?
Declare @teamcount int, @divisiontotal int, @currentdivision int, @matchcount int, @matchkey int, @matchdate datetime, @awayteamkey int, @hometeamkey int, @teamkey int
Set @matchcount = 0
/* cursor loop */
while @matchcount < 6
begin
Declare teamkeys_cursor Cursor FOR
Select teamkey
From teamtable
where thursdayflag = 1
and division = 2
OPEN teamkeys_cursor /* open cursor */
Fetch NEXT FROM teamkeys_cursor
INTO @teamkey
Set @matchdate = Dateadd(day, 7*@matchcount, '12/26/2013')
WHILE (@@FETCH_STATUS <> -1)
BEGIN
Insert into matchtable (HomeTeamKey, MatchDate) Values (@teamkey, @matchdate)
Fetch NEXT FROM teamkeys_cursor
INTO @teamkey
END
/* release data structures that was allocated by cursor */
CLOSE teamkeys_cursor
DEALLOCATE teamkeys_cursor
Declare match_cursor Cursor FOR
Select hometeamkey, matchkey
From matchtable
where matchdate = @matchdate
order by matchkey desc
Set @teamcount = 1
OPEN match_cursor /* open cursor */
Fetch NEXT FROM match_cursor
INTO @teamkey, @matchkey
WHILE (@@FETCH_STATUS <> -1)
BEGIN
Update top (1) matchtable
Set Awayteamkey = foo.hometeamkey
FROM (
SELECT
ROW_NUMBER() OVER (ORDER BY matchkey desc) AS rownumber, hometeamkey
FROM MatchTable
Where hometeamkey <> @teamkey and awayteamkey is null
) AS foo
WHERE rownumber = @teamcount
Set @teamcount = @teamcount + 1
Fetch NEXT FROM match_cursor
INTO @teamkey, @matchkey
END
/* release data structures that was allocated by cursor */
CLOSE match_cursor
DEALLOCATE match_cursor
--print @matchcount
Set @matchcount = @matchcount + 1
end