我如何计算我的体育联盟的赛程安排

时间:2017-04-21 10:24:51

标签: sql loops schedule fixtures timetable

我经营着一个有3个师的小型体育联盟。女士们更高,女士们更低,男士师。根据分区中的球队数量,他们可以互相比赛一次,也可以互相比赛两次。

联赛持续超过12周(每周一晚)(如果进入的球队较少,则为更短的周数)。每晚有24场比赛,4个球场有6个时段。

由于比赛只有20分钟,每支球队每晚将进行2场比赛,如果在联赛过程中有一些奇数的比赛,最多可以达到3场比赛。每个星期都可以混合使用每个分区,也可以让分区休息一周,但最好至少有两个分区在任何一周都有比赛。

我已经构建了一个包含所有需要播放的匹配的表格:

CREATE TABLE [dbo].[TestMatches](
    [UniqueMatchId] [int] IDENTITY(1,1) NOT NULL,
    [HomeTeam] [varchar](4) NULL,
    [AwayTeam] [varchar](4) NULL
)

INSERT INTO dbo.TestMatches
([HomeTeam]
,[AwayTeam])
VALUES
('LH1', 'LH2')
,('LH1', 'LH3')
,('LH1', 'LH4')
,('LH1', 'LH5')
,('LH1', 'LH6')
,('LH1', 'LH7')
,('LH1', 'LH8')
,('LH1', 'LH9')
,('LH1', 'LH10')
,('LH2', 'LH3')
,('LH2', 'LH4')
,('LH2', 'LH5')
,('LH2', 'LH6')
,('LH2', 'LH7')
,('LH2', 'LH8')
,('LH2', 'LH9')
,('LH2', 'LH10')
,('LH3', 'LH4')
,('LH3', 'LH5')
,('LH3', 'LH6')
,('LH3', 'LH7')
,('LH3', 'LH8')
,('LH3', 'LH9')
,('LH3', 'LH10')
,('LH4', 'LH5')
,('LH4', 'LH6')
,('LH4', 'LH7')
,('LH4', 'LH8')
,('LH4', 'LH9')
,('LH4', 'LH10')
,('LH5', 'LH6')
,('LH5', 'LH7')
,('LH5', 'LH8')
,('LH5', 'LH9')
,('LH5', 'LH10')
,('LH6', 'LH7')
,('LH6', 'LH8')
,('LH6', 'LH9')
,('LH6', 'LH10')
,('LH7', 'LH8')
,('LH7', 'LH9')
,('LH7', 'LH10')
,('LH8', 'LH9')
,('LH8', 'LH10')
,('LH9', 'LH10')
,('LH2', 'LH1')
,('LH3', 'LH1')
,('LH4', 'LH1')
,('LH5', 'LH1')
,('LH6', 'LH1')
,('LH7', 'LH1')
,('LH8', 'LH1')
,('LH9', 'LH1')
,('LH10', 'LH1')
,('LH3', 'LH2')
,('LH4', 'LH2')
,('LH5', 'LH2')
,('LH6', 'LH2')
,('LH7', 'LH2')
,('LH8', 'LH2')
,('LH9', 'LH2')
,('LH10', 'LH2')
,('LH4', 'LH3')
,('LH5', 'LH3')
,('LH6', 'LH3')
,('LH7', 'LH3')
,('LH8', 'LH3')
,('LH9', 'LH3')
,('LH10', 'LH3')
,('LH5', 'LH4')
,('LH6', 'LH4')
,('LH7', 'LH4')
,('LH8', 'LH4')
,('LH9', 'LH4')
,('LH10', 'LH4')
,('LH6', 'LH5')
,('LH7', 'LH5')
,('LH8', 'LH5')
,('LH9', 'LH5')
,('LH10', 'LH5')
,('LH7', 'LH6')
,('LH8', 'LH6')
,('LH9', 'LH6')
,('LH10', 'LH6')
,('LH8', 'LH7')
,('LH9', 'LH7')
,('LH10', 'LH7')
,('LH9', 'LH8')
,('LH10', 'LH8')
,('LH10', 'LH9')
,('LL1', 'LL2')
,('LL1', 'LL3')
,('LL1', 'LL4')
,('LL1', 'LL5')
,('LL1', 'LL6')
,('LL1', 'LL7')
,('LL1', 'LL8')
,('LL1', 'LL9')
,('LL1', 'LL10')
,('LL1', 'LL11')
,('LL1', 'LL12')
,('LL1', 'LL13')
,('LL1', 'LL14')
,('LL2', 'LL3')
,('LL2', 'LL4')
,('LL2', 'LL5')
,('LL2', 'LL6')
,('LL2', 'LL7')
,('LL2', 'LL8')
,('LL2', 'LL9')
,('LL2', 'LL10')
,('LL2', 'LL11')
,('LL2', 'LL12')
,('LL2', 'LL13')
,('LL2', 'LL14')
,('LL3', 'LL4')
,('LL3', 'LL5')
,('LL3', 'LL6')
,('LL3', 'LL7')
,('LL3', 'LL8')
,('LL3', 'LL9')
,('LL3', 'LL10')
,('LL3', 'LL11')
,('LL3', 'LL12')
,('LL3', 'LL13')
,('LL3', 'LL14')
,('LL4', 'LL5')
,('LL4', 'LL6')
,('LL4', 'LL7')
,('LL4', 'LL8')
,('LL4', 'LL9')
,('LL4', 'LL10')
,('LL4', 'LL11')
,('LL4', 'LL12')
,('LL4', 'LL13')
,('LL4', 'LL14')
,('LL5', 'LL6')
,('LL5', 'LL7')
,('LL5', 'LL8')
,('LL5', 'LL9')
,('LL5', 'LL10')
,('LL5', 'LL11')
,('LL5', 'LL12')
,('LL5', 'LL13')
,('LL5', 'LL14')
,('LL6', 'LL7')
,('LL6', 'LL8')
,('LL6', 'LL9')
,('LL6', 'LL10')
,('LL6', 'LL11')
,('LL6', 'LL12')
,('LL6', 'LL13')
,('LL6', 'LL14')
,('LL7', 'LL8')
,('LL7', 'LL9')
,('LL7', 'LL10')
,('LL7', 'LL11')
,('LL7', 'LL12')
,('LL7', 'LL13')
,('LL7', 'LL14')
,('LL8', 'LL9')
,('LL8', 'LL10')
,('LL8', 'LL11')
,('LL8', 'LL12')
,('LL8', 'LL13')
,('LL8', 'LL14')
,('LL9', 'LL10')
,('LL9', 'LL11')
,('LL9', 'LL12')
,('LL9', 'LL13')
,('LL9', 'LL14')
,('LL10', 'LL11')
,('LL10', 'LL12')
,('LL10', 'LL13')
,('LL10', 'LL14')
,('LL11', 'LL12')
,('LL11', 'LL13')
,('LL11', 'LL14')
,('LL12', 'LL13')
,('LL12', 'LL14')
,('LL13', 'LL14')
,('M1', 'M2')
,('M1', 'M3')
,('M1', 'M4')
,('M1', 'M5')
,('M1', 'M6')
,('M1', 'M7')
,('M1', 'M8')
,('M1', 'M9')
,('M2', 'M3')
,('M2', 'M4')
,('M2', 'M5')
,('M2', 'M6')
,('M2', 'M7')
,('M2', 'M8')
,('M2', 'M9')
,('M3', 'M4')
,('M3', 'M5')
,('M3', 'M6')
,('M3', 'M7')
,('M3', 'M8')
,('M3', 'M9')
,('M4', 'M5')
,('M4', 'M6')
,('M4', 'M7')
,('M4', 'M8')
,('M4', 'M9')
,('M5', 'M6')
,('M5', 'M7')
,('M5', 'M8')
,('M5', 'M9')
,('M6', 'M7')
,('M6', 'M8')
,('M6', 'M9')
,('M7', 'M8')
,('M7', 'M9')
,('M8', 'M9')
,('M2', 'M1')
,('M3', 'M1')
,('M4', 'M1')
,('M5', 'M1')
,('M6', 'M1')
,('M7', 'M1')
,('M8', 'M1')
,('M9', 'M1')
,('M3', 'M2')
,('M4', 'M2')
,('M5', 'M2')
,('M6', 'M2')
,('M7', 'M2')
,('M8', 'M2')
,('M9', 'M2')
,('M4', 'M3')
,('M5', 'M3')
,('M6', 'M3')
,('M7', 'M3')
,('M8', 'M3')
,('M9', 'M3')
,('M5', 'M4')
,('M6', 'M4')
,('M7', 'M4')
,('M8', 'M4')
,('M9', 'M4')
,('M6', 'M5')
,('M7', 'M5')
,('M8', 'M5')
,('M9', 'M5')
,('M7', 'M6')
,('M8', 'M6')
,('M9', 'M6')
,('M8', 'M7')
,('M9', 'M7')
,('M9', 'M8')

我还构建了一个包含所有可用时隙的表格:

CREATE TABLE dbo.TestPitchTimes(
[UniqueSlotId] [int] IDENTITY(1,1) NOT NULL,
WeekNo varchar(6) NULL,
PitchNo varchar(2) NULL,
TimeSlot char(4) NULL
)

INSERT INTO dbo.TestPitchTimes
(WeekNo,
PitchNo,
TimeSlot
)
VALUES
('Week1', 'P1', '1900')
,('Week1', 'P1', '1925')
,('Week1', 'P1', '1950')
,('Week1', 'P1', '2015')
,('Week1', 'P1', '2040')
,('Week1', 'P1', '2105')
,('Week1', 'P2', '1900')
,('Week1', 'P2', '1925')
,('Week1', 'P2', '1950')
,('Week1', 'P2', '2015')
,('Week1', 'P2', '2040')
,('Week1', 'P2', '2105')
,('Week1', 'P3', '1900')
,('Week1', 'P3', '1925')
,('Week1', 'P3', '1950')
,('Week1', 'P3', '2015')
,('Week1', 'P3', '2040')
,('Week1', 'P3', '2105')
,('Week1', 'P4', '1900')
,('Week1', 'P4', '1925')
,('Week1', 'P4', '1950')
,('Week1', 'P4', '2015')
,('Week1', 'P4', '2040')
,('Week1', 'P4', '2105')
,('Week2', 'P1', '1900')
,('Week2', 'P1', '1925')
,('Week2', 'P1', '1950')
,('Week2', 'P1', '2015')
,('Week2', 'P1', '2040')
,('Week2', 'P1', '2105')
,('Week2', 'P2', '1900')
,('Week2', 'P2', '1925')
,('Week2', 'P2', '1950')
,('Week2', 'P2', '2015')
,('Week2', 'P2', '2040')
,('Week2', 'P2', '2105')
,('Week2', 'P3', '1900')
,('Week2', 'P3', '1925')
,('Week2', 'P3', '1950')
,('Week2', 'P3', '2015')
,('Week2', 'P3', '2040')
,('Week2', 'P3', '2105')
,('Week2', 'P4', '1900')
,('Week2', 'P4', '1925')
,('Week2', 'P4', '1950')
,('Week2', 'P4', '2015')
,('Week2', 'P4', '2040')
,('Week2', 'P4', '2105')
,('Week3', 'P1', '1900')
,('Week3', 'P1', '1925')
,('Week3', 'P1', '1950')
,('Week3', 'P1', '2015')
,('Week3', 'P1', '2040')
,('Week3', 'P1', '2105')
,('Week3', 'P2', '1900')
,('Week3', 'P2', '1925')
,('Week3', 'P2', '1950')
,('Week3', 'P2', '2015')
,('Week3', 'P2', '2040')
,('Week3', 'P2', '2105')
,('Week3', 'P3', '1900')
,('Week3', 'P3', '1925')
,('Week3', 'P3', '1950')
,('Week3', 'P3', '2015')
,('Week3', 'P3', '2040')
,('Week3', 'P3', '2105')
,('Week3', 'P4', '1900')
,('Week3', 'P4', '1925')
,('Week3', 'P4', '1950')
,('Week3', 'P4', '2015')
,('Week3', 'P4', '2040')
,('Week3', 'P4', '2105')
,('Week4', 'P1', '1900')
,('Week4', 'P1', '1925')
,('Week4', 'P1', '1950')
,('Week4', 'P1', '2015')
,('Week4', 'P1', '2040')
,('Week4', 'P1', '2105')
,('Week4', 'P2', '1900')
,('Week4', 'P2', '1925')
,('Week4', 'P2', '1950')
,('Week4', 'P2', '2015')
,('Week4', 'P2', '2040')
,('Week4', 'P2', '2105')
,('Week4', 'P3', '1900')
,('Week4', 'P3', '1925')
,('Week4', 'P3', '1950')
,('Week4', 'P3', '2015')
,('Week4', 'P3', '2040')
,('Week4', 'P3', '2105')
,('Week4', 'P4', '1900')
,('Week4', 'P4', '1925')
,('Week4', 'P4', '1950')
,('Week4', 'P4', '2015')
,('Week4', 'P4', '2040')
,('Week4', 'P4', '2105')
,('Week5', 'P1', '1900')
,('Week5', 'P1', '1925')
,('Week5', 'P1', '1950')
,('Week5', 'P1', '2015')
,('Week5', 'P1', '2040')
,('Week5', 'P1', '2105')
,('Week5', 'P2', '1900')
,('Week5', 'P2', '1925')
,('Week5', 'P2', '1950')
,('Week5', 'P2', '2015')
,('Week5', 'P2', '2040')
,('Week5', 'P2', '2105')
,('Week5', 'P3', '1900')
,('Week5', 'P3', '1925')
,('Week5', 'P3', '1950')
,('Week5', 'P3', '2015')
,('Week5', 'P3', '2040')
,('Week5', 'P3', '2105')
,('Week5', 'P4', '1900')
,('Week5', 'P4', '1925')
,('Week5', 'P4', '1950')
,('Week5', 'P4', '2015')
,('Week5', 'P4', '2040')
,('Week5', 'P4', '2105')
,('Week6', 'P1', '1900')
,('Week6', 'P1', '1925')
,('Week6', 'P1', '1950')
,('Week6', 'P1', '2015')
,('Week6', 'P1', '2040')
,('Week6', 'P1', '2105')
,('Week6', 'P2', '1900')
,('Week6', 'P2', '1925')
,('Week6', 'P2', '1950')
,('Week6', 'P2', '2015')
,('Week6', 'P2', '2040')
,('Week6', 'P2', '2105')
,('Week6', 'P3', '1900')
,('Week6', 'P3', '1925')
,('Week6', 'P3', '1950')
,('Week6', 'P3', '2015')
,('Week6', 'P3', '2040')
,('Week6', 'P3', '2105')
,('Week6', 'P4', '1900')
,('Week6', 'P4', '1925')
,('Week6', 'P4', '1950')
,('Week6', 'P4', '2015')
,('Week6', 'P4', '2040')
,('Week6', 'P4', '2105')
,('Week7', 'P1', '1900')
,('Week7', 'P1', '1925')
,('Week7', 'P1', '1950')
,('Week7', 'P1', '2015')
,('Week7', 'P1', '2040')
,('Week7', 'P1', '2105')
,('Week7', 'P2', '1900')
,('Week7', 'P2', '1925')
,('Week7', 'P2', '1950')
,('Week7', 'P2', '2015')
,('Week7', 'P2', '2040')
,('Week7', 'P2', '2105')
,('Week7', 'P3', '1900')
,('Week7', 'P3', '1925')
,('Week7', 'P3', '1950')
,('Week7', 'P3', '2015')
,('Week7', 'P3', '2040')
,('Week7', 'P3', '2105')
,('Week7', 'P4', '1900')
,('Week7', 'P4', '1925')
,('Week7', 'P4', '1950')
,('Week7', 'P4', '2015')
,('Week7', 'P4', '2040')
,('Week7', 'P4', '2105')
,('Week8', 'P1', '1900')
,('Week8', 'P1', '1925')
,('Week8', 'P1', '1950')
,('Week8', 'P1', '2015')
,('Week8', 'P1', '2040')
,('Week8', 'P1', '2105')
,('Week8', 'P2', '1900')
,('Week8', 'P2', '1925')
,('Week8', 'P2', '1950')
,('Week8', 'P2', '2015')
,('Week8', 'P2', '2040')
,('Week8', 'P2', '2105')
,('Week8', 'P3', '1900')
,('Week8', 'P3', '1925')
,('Week8', 'P3', '1950')
,('Week8', 'P3', '2015')
,('Week8', 'P3', '2040')
,('Week8', 'P3', '2105')
,('Week8', 'P4', '1900')
,('Week8', 'P4', '1925')
,('Week8', 'P4', '1950')
,('Week8', 'P4', '2015')
,('Week8', 'P4', '2040')
,('Week8', 'P4', '2105')
,('Week9', 'P1', '1900')
,('Week9', 'P1', '1925')
,('Week9', 'P1', '1950')
,('Week9', 'P1', '2015')
,('Week9', 'P1', '2040')
,('Week9', 'P1', '2105')
,('Week9', 'P2', '1900')
,('Week9', 'P2', '1925')
,('Week9', 'P2', '1950')
,('Week9', 'P2', '2015')
,('Week9', 'P2', '2040')
,('Week9', 'P2', '2105')
,('Week9', 'P3', '1900')
,('Week9', 'P3', '1925')
,('Week9', 'P3', '1950')
,('Week9', 'P3', '2015')
,('Week9', 'P3', '2040')
,('Week9', 'P3', '2105')
,('Week9', 'P4', '1900')
,('Week9', 'P4', '1925')
,('Week9', 'P4', '1950')
,('Week9', 'P4', '2015')
,('Week9', 'P4', '2040')
,('Week9', 'P4', '2105')
,('Week10', 'P1', '1900')
,('Week10', 'P1', '1925')
,('Week10', 'P1', '1950')
,('Week10', 'P1', '2015')
,('Week10', 'P1', '2040')
,('Week10', 'P1', '2105')
,('Week10', 'P2', '1900')
,('Week10', 'P2', '1925')
,('Week10', 'P2', '1950')
,('Week10', 'P2', '2015')
,('Week10', 'P2', '2040')
,('Week10', 'P2', '2105')
,('Week10', 'P3', '1900')
,('Week10', 'P3', '1925')
,('Week10', 'P3', '1950')
,('Week10', 'P3', '2015')
,('Week10', 'P3', '2040')
,('Week10', 'P3', '2105')
,('Week10', 'P4', '1900')
,('Week10', 'P4', '1925')
,('Week10', 'P4', '1950')
,('Week10', 'P4', '2015')
,('Week10', 'P4', '2040')
,('Week10', 'P4', '2105')
,('Week11', 'P1', '1900')
,('Week11', 'P1', '1925')
,('Week11', 'P1', '1950')
,('Week11', 'P1', '2015')
,('Week11', 'P1', '2040')
,('Week11', 'P1', '2105')
,('Week11', 'P2', '1900')
,('Week11', 'P2', '1925')
,('Week11', 'P2', '1950')
,('Week11', 'P2', '2015')
,('Week11', 'P2', '2040')
,('Week11', 'P2', '2105')
,('Week11', 'P3', '1900')
,('Week11', 'P3', '1925')
,('Week11', 'P3', '1950')
,('Week11', 'P3', '2015')
,('Week11', 'P3', '2040')
,('Week11', 'P3', '2105')
,('Week11', 'P4', '1900')
,('Week11', 'P4', '1925')
,('Week11', 'P4', '1950')
,('Week11', 'P4', '2015')
,('Week11', 'P4', '2040')
,('Week11', 'P4', '2105')
,('Week12', 'P1', '1900')
,('Week12', 'P1', '1925')
,('Week12', 'P1', '1950')
,('Week12', 'P1', '2015')
,('Week12', 'P1', '2040')
,('Week12', 'P1', '2105')
,('Week12', 'P2', '1900')
,('Week12', 'P2', '1925')
,('Week12', 'P2', '1950')
,('Week12', 'P2', '2015')
,('Week12', 'P2', '2040')
,('Week12', 'P2', '2105')
,('Week12', 'P3', '1900')
,('Week12', 'P3', '1925')
,('Week12', 'P3', '1950')
,('Week12', 'P3', '2015')
,('Week12', 'P3', '2040')
,('Week12', 'P3', '2105')
,('Week12', 'P4', '1900')
,('Week12', 'P4', '1925')
,('Week12', 'P4', '1950')
,('Week12', 'P4', '2015')
,('Week12', 'P4', '2040')
,('Week12', 'P4', '2105')

我需要做的是运行一个循环(可能?),为每周分配匹配,以便团队每周玩> gt = = 2或< = 3个匹配(最大化玩2的周数)如果在联赛期间互相比赛两次,则不要在同一周内主场和客场比赛。

我对SQL很陌生,我觉得这是可能的,但我不知道这样做的代码,而且我找不到任何符合我联盟复杂性的东西!可能是学校时间表,4个教室,6个课时以及每个可用插槽中需要的学生对的类似问题?

我目前正在手动制定计划,每年需要很长时间,因为团队的数量总是在变化,因此我无法重复使用以前的计划。我希望这可以加快这个过程。非常感谢您提供任何帮助。

编辑:我希望看到的结果是:

WeekNo,UniqueMatchId

第1周,1

第1,2周

第1周,10 ......等

我首先想要确定每周玩哪些游戏,然后计算每个游戏应该播放的时间(因此团队不会同时玩2场游戏)但这是另一个问题!

0 个答案:

没有答案