-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries.sql
157 lines (137 loc) · 5.81 KB
/
queries.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
153
154
155
156
157
-- Players whose spring 2023 Glicko score increased compared to a year ago
CREATE TABLE spring2023_vs_spring2022 AS (
SELECT s2023.player,
s2023.rank AS spring_2023_rank,
s2023.points AS spring_2023_points,
f2022.rank AS fall_2022_rank,
f2022.points AS fall_2022_points
FROM spring_2023_ranking AS s2023
INNER JOIN fall_2022_ranking AS f2022
ON (f2022.player = s2023.player)
WHERE (s2023.points > f2022.points)
);
-- Players whose spring 2023 Glicko score increased compared to the previous semester
-- (fall 2022)
CREATE TABLE spring2023_vs_fall_2022 AS (
SELECT s2023.player,
s2023.rank AS spring_2023_rank,
s2023.points AS spring_2023_points,
s2022.rank AS spring_2022_rank,
s2022.points AS spring_2022_points
FROM spring_2023_ranking AS s2023
INNER JOIN spring_2022_ranking AS s2022
ON (s2022.player = s2023.player)
WHERE (s2023.points > s2022.points)
);
-- Players who were active in both the very first and very latest semesters
CREATE TABLE active_since_beginning AS (
SELECT s2023.player
FROM spring_2023_ranking AS s2023
INNER JOIN spring_2019_ranking AS s2019
ON (s2019.player = s2023.player)
);
-- Creating a new table combining rankings for every semester
CREATE TABLE all_rankings AS (
SELECT "player", "rank", "points", 'Spring 2019' AS semester FROM spring_2019_ranking
UNION ALL
SELECT "player", "rank", "points", 'Fall 2019' AS semester FROM fall_2019_ranking
UNION ALL
SELECT "player", "rank", "points", 'Spring 2020' AS semester FROM spring_2020_ranking
UNION ALL
SELECT "player", "rank", "points", 'Fall 2021' AS semester FROM fall_2021_ranking
UNION ALL
SELECT "player", "rank", "points", 'Spring 2022' AS semester FROM spring_2022_ranking
UNION ALL
SELECT "player", "rank", "points", 'Fall 2022' AS semester FROM fall_2022_ranking
UNION ALL
SELECT "player", "rank", "points", 'Spring 2023' AS semester FROM spring_2023_ranking
);
-- Players whose Glicko score has ever exceeded 1900 points,
-- ordered from highest to lowest peak score
CREATE TABLE glicko_above_1900 AS (
SELECT "player", semester, "rank", "points"
FROM all_rankings
WHERE "points" > 1900
ORDER BY "points" DESC, semester
);
-- Peak Glicko scores for every player, ordered from highest to lowest
CREATE TABLE peak_glicko_scores AS (
WITH Ranked AS (
SELECT "player", semester, "rank", "points" AS peak_points,
ROW_NUMBER() OVER (PARTITION BY "player" ORDER BY "points" DESC) AS rn
FROM all_rankings
)
SELECT "player", semester, "rank", peak_points
FROM Ranked
WHERE rn = 1
ORDER BY peak_points DESC);
-- Peak rank for every player, ordered from highest to lowest
CREATE TABLE peak_ranks AS (
WITH Ranked AS (
SELECT "player", semester, "rank" AS peak_rank, "points",
ROW_NUMBER() OVER (PARTITION BY "player" ORDER BY "rank") AS rn
FROM all_rankings
)
SELECT "player", semester, peak_rank, "points"
FROM Ranked
WHERE rn = 1
ORDER BY peak_rank);
-- Every player that reached their peak Glicko score in spring 2023
CREATE TABLE peaked_in_spring2023 AS (
SELECT "player", "rank", "peak_points"
FROM peak_glicko_scores
WHERE semester = 'Spring 2023');
-- Every player that reached their peak Glicko score at the beginning of the game
CREATE TABLE peaked_in_spring2019 AS (
SELECT "player", "rank", "peak_points"
FROM peak_glicko_scores
WHERE semester = 'Spring 2019');
-- Every player that reached their peak Glicko score pre-quarantine
CREATE TABLE peaked_pre_quarantine AS (
SELECT "player", semester, "rank", "peak_points"
FROM peak_glicko_scores
WHERE semester = 'Spring 2019' OR semester = 'Fall 2019' OR semester = 'Spring 2020');
-- Every player that reached their peak Glicko score post-quarantine
CREATE TABLE peaked_post_quarantine AS (
SELECT "player", semester, "rank", "peak_points"
FROM peak_glicko_scores
WHERE semester = 'Fall 2021' OR semester = 'Spring 2022' OR semester = 'Fall 2022' OR semester = 'Spring 2023');
-- Every player who has ever been ranked in the top 10
CREATE TABLE ever_ranked_top10 AS (
SELECT "player", semester, "rank", "points"
FROM all_rankings
WHERE "rank" <= 10
ORDER BY "player", "semester");
-- Every player who hit their peak rank post-quarantine
CREATE TABLE peak_rank_post_quarantine AS (
SELECT "player", semester, "peak_rank", "points"
FROM peak_ranks
WHERE semester = 'Fall 2021' OR semester = 'Spring 2022' OR semester = 'Fall 2022' OR semester = 'Spring 2023');
-- Players who hit their peak score AND rank pre-quarantine
CREATE TABLE peak_score_and_rank_pre_quarantine AS (
SELECT pg.player, pr.peak_rank, pg.peak_points, pr.semester AS peak_rank_semester, pg.semester AS peak_points_semester
FROM peak_glicko_scores AS pg
INNER JOIN peak_ranks AS pr
ON (pg.player = pr.player)
WHERE (pg.semester = 'Spring 2019' OR pg.semester = 'Fall 2019' OR pg.semester = 'Spring 2020')
ORDER BY pr.peak_rank);
-- Players who hit their peak score AND rank post-quarantine
CREATE TABLE peak_score_and_rank_post_quarantine AS (
SELECT pg.player, pr.peak_rank, pg.peak_points, pr.semester AS peak_rank_semester, pg.semester AS peak_points_semester
FROM peak_glicko_scores AS pg
INNER JOIN peak_ranks AS pr
ON (pg.player = pr.player)
WHERE (pg.semester = 'Fall 2021' OR pg.semester = 'Spring 2022' OR pg.semester = 'Fall 2022' OR pg.semester = 'Spring 2023')
ORDER BY pr.peak_rank);
-- A full table of every players' peak scores and ranks, and when they earned them
CREATE TABLE peak_scores_and_ranks AS (
SELECT pg.player, pr.peak_rank, pg.peak_points, pr.semester AS peak_rank_semester, pg.semester AS peak_points_semester
FROM peak_glicko_scores AS pg
INNER JOIN peak_ranks AS pr
ON (pg.player = pr.player)
);
-- All players who had their peak Glicko score and rank in the same semester
CREATE TABLE peak_score_rank_same_semester AS (
SELECT "player", "peak_rank", "peak_points", "peak_rank_semester" AS "semester"
FROM peak_scores_and_ranks
WHERE "peak_rank_semester" = "peak_points_semester");