Sql-Server

添加基於 Datefield 的 Gameweek Fixeture

  • October 10, 2019

擁有下表,我需要根據表中的日期列添加一個名為“Round”的新列來表示第 1 輪、第 2 輪等。應該對在 3 天時間跨度內發生的比賽進行同一輪比賽(要使用一些日期差異,無法弄清楚)

初始表

Date            HomeTeam     AwayTeam
2018-08-10     Man United    Leicester
2018-08-11     Bournemouth   Cardiff
2018-08-11      Fulham       Crystal Palace
2018-08-11    Huddersfield   Chelsea
2018-08-11      Newcastle    Tottenham
2018-08-11       Watford     Brighton
2018-08-11       Wolves      Everton
2018-08-12      Arsenal      Man City
2018-08-12     Liverpool     West Ham
2018-08-12    Southampton    Burnley
2018-08-18   Cardiff         Newcastle
2018-08-18     Chelsea       Arsenal
2018-08-18     Everton       Southampton

期望的輸出

Date            HomeTeam     AwayTeam          RoundNumber
2018-08-10     Man United    Leicester         1
2018-08-11     Bournemouth   Cardiff           1
2018-08-11      Fulham       Crystal Palace    1
2018-08-11    Huddersfield   Chelsea           1
2018-08-11      Newcastle    Tottenham         1
2018-08-11       Watford     Brighton          1
2018-08-11       Wolves      Everton           1
2018-08-12      Arsenal      Man City          1
2018-08-12     Liverpool     West Ham          1
2018-08-12    Southampton    Burnley           1
2018-08-18    Cardiff        Newcastle         2
2018-08-18     Chelsea       Arsenal           2
2018-08-18     Everton       Southampton       2

如果比賽之間的間隔總是超過 3 天,你可以使用這個:

;WITH CTE AS
(
SELECT  DateField,
       CASE WHEN DATEDIFF(day,LAG(DateField) OVER(ORDER BY DateField),DateField)> 3 THEN 1 ELSE 0 END as newround,
       Hometeam,
       AwayTeam
FROM dbo.Matches

)
SELECT Datefield, HomeTeam,AwayTeam, SUM(newround) OVER(ORDER BY DateField) + 1 as [Round]
FROM CTE;

DDL 和 DML

CREATE TABLE dbo.Matches(DateField DATE,HomeTeam varchar(100),AwayTeam varchar(100));
INSERT INTO dbo.Matches
VALUES

('2018-08-10',   'Man United',    'Leicester'),
('2018-08-11',   'Bournemouth',   'Cardiff'),
('2018-08-11',   'Fulham',       'Crystal Palace'),
('2018-08-11',   'Huddersfield',   'Chelsea'),
('2018-08-11',   'Newcastle',    'Tottenham'),
('2018-08-11',   'Watford',     'Brighton'),
('2018-08-11',   'Wolves',      'Everton'),
('2018-08-12',   'Arsenal',      'Man City'),
('2018-08-12',   'Liverpool',     'West Ham'),
('2018-08-12',   'Southampton',    'Burnley'),
('2018-08-18',   'Cardiff',         'Newcastle'),
('2018-08-18',   'Chelsea',       'Arsenal'),
('2018-08-18',   'Everton',       'Southampton'),
('2018-08-25',   'Round3Example_1',       'Round3Example_2');

結果

Datefield   HomeTeam              AwayTeam            Round
2018-08-10  Man United            Leicester           1
2018-08-11  Bournemouth           Cardiff             1
2018-08-11  Fulham                Crystal Palace      1
2018-08-11  Huddersfield          Chelsea             1
2018-08-11  Newcastle             Tottenham           1
2018-08-11  Watford               Brighton            1
2018-08-11  Wolves                Everton             1
2018-08-12  Arsenal               Man City            1
2018-08-12  Liverpool             West Ham            1
2018-08-12  Southampton           Burnley             1
2018-08-18  Cardiff               Newcastle           2
2018-08-18  Chelsea               Arsenal             2
2018-08-18  Everton               Southampton         2
2018-08-25  Round3Example_1       Round3Example_2     3

Db<>小提琴

引用自:https://dba.stackexchange.com/questions/250736