-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLeague Table Case Study.sql
152 lines (135 loc) · 3.18 KB
/
League Table Case Study.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Advanced - League Table Case Study
Build the league table in a few steps
Firstly get each team results any individual match level
and then summarise into a basic league table
and store as a temporary table
*/
DROP TABLE IF EXISTS #LeagueTable;
WITH MatchResult ([Date], [Team], [Won], [Drawn], [Lost], [For], [Against], [Points])
AS
(
SELECT
m.Date
, m.HomeTeam AS Team
, CASE WHEN m.FTHG > m.FTAG THEN 1 ELSE 0 END AS Won
, CASE WHEN m.FTHG = m.FTAG THEN 1 ELSE 0 END AS Drawn
, CASE WHEN m.FTHG < m.FTAG THEN 1 ELSE 0 END AS Lost
, m.FTHG AS [For]
, m.FTAG AS [Against]
, CASE m.FTR WHEN 'H' THEN 3 WHEN 'D' THEN 1 ELSE 0 END AS Points
FROM
dbo.FootballMatch m
UNION ALL
SELECT
m.Date,
m.AwayTeam AS Team,
CASE WHEN m.FTHG < m.FTAG THEN 1 ELSE 0 END AS Won,
CASE WHEN m.FTHG = m.FTAG THEN 1 ELSE 0 END AS Drawn,
CASE WHEN m.FTHG > m.FTAG THEN 1 ELSE 0 END AS Lost,
m.FTAG AS [For],
m.FTHG AS [Against],
CASE m.FTR WHEN 'H' THEN 0 WHEN 'D' THEN 1 ELSE 3 END AS Points
FROM dbo.FootballMatch m
)
--SELECT * FROM MatchResult
SELECT
Team
, SUM(Won) AS Won
, SUM(Drawn) AS Drawn
, SUM(Lost) AS Lost
, SUM([For]) AS [For]
, SUM(Against) AS Against
, SUM(Points) AS Points
INTO
#LeagueTable
FROM
MatchResult
GROUP BY
Team;
SELECT
Team
, Won
, Drawn
, Lost
, [For]
, Against
, ([For] - Against) AS GD
, Points
FROM
#LeagueTable
ORDER BY
Points DESC
, GD DESC;
-- Optional � add the Form, the team�s results of the last five games
DROP TABLE IF EXISTS #MatchForm;
WITH MatchResult ([Date], [Team], [Result])
AS
(
SELECT
m.Date
, m.HomeTeam AS Team
, CASE m.FTR WHEN 'H' THEN 'W' WHEN 'D' THEN 'D' ELSE 'L' END AS Result
FROM dbo.FootballMatch m
UNION ALL
SELECT
m.Date
, m.AwayTeam AS Team
,CASE m.FTR WHEN 'H' THEN 'L' WHEN 'D' THEN 'D' ELSE 'W' END AS Result
FROM dbo.FootballMatch m
)
SELECT
[Team]
, [Date]
, [Result]
, ROW_NUMBER() OVER (PARTITION BY Team ORDER BY Team , [Date] DESC) AS ReverseDateRank
INTO
#MatchForm
FROM
MatchResult;
SELECT * FROM #MatchForm;
-- we only need the results frtom the last 5 matches
DELETE FROM #MatchForm WHERE ReverseDateRank > 5;
SELECT * FROM #MatchForm;
DROP TABLE IF EXISTS #MatchFormTranspose;
SELECT
Team
, CASE WHEN ReverseDateRank = 1 THEN Result ELSE NULL END AS R1
, CASE WHEN ReverseDateRank = 2 THEN Result ELSE NULL END AS R2
, CASE WHEN ReverseDateRank = 3 THEN Result ELSE NULL END AS R3
, CASE WHEN ReverseDateRank = 4 THEN Result ELSE NULL END AS R4
, CASE WHEN ReverseDateRank = 5 THEN Result ELSE NULL END AS R5
INTO #MatchFormTranspose
FROM #MatchForm;
SELECT * FROM #MatchFormTranspose;
DROP TABLE IF EXISTS #MatchFormTransposeGroup;
SELECT
Team
, MAX(R1) AS R1
, MAX(R2) AS R2
, MAX(R3) AS R3
, MAX(R4) AS R4
, MAX(R5) AS R5
, MAX(R5) + MAX(R4) + MAX(R3) + MAX(R2) + MAX(R1) AS LatestMatchForm
INTO
#MatchFormTransposeGroup
FROM
#MatchFormTranspose
GROUP BY
Team;
SELECT * FROM #MatchFormTransposeGroup;
SELECT
lt.Team
, lt.Won
, lt.Drawn
, lt.Lost
, lt.[For]
, lt.Against
, (lt.[For] - lt.Against) AS GD
, lt.Points
, mftg.LatestMatchForm
FROM
#LeagueTable lt INNER JOIN #MatchFormTransposeGroup mftg ON mftg.Team = lt.Team
ORDER BY
lt.Points DESC
, GD DESC;