-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery2.sql
More file actions
331 lines (307 loc) · 6.67 KB
/
Copy pathquery2.sql
File metadata and controls
331 lines (307 loc) · 6.67 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
SET
SEARCH_PATH TO Speedrun;
DROP VIEW IF EXISTS YearMonthRun CASCADE;
DROP VIEW IF EXISTS AvgMonthlyRunTimePerGameCatPerYear CASCADE;
DROP VIEW IF EXISTS AllYears CASCADE;
DROP VIEW IF EXISTS AllItemsAllYears CASCADE;
DROP VIEW IF EXISTS AllAvgMonthlyRunTimes CASCADE;
DROP VIEW IF EXISTS GameIds CASCADE;
DROP VIEW IF EXISTS Leaderboard CASCADE;
CREATE VIEW YearMonthRun AS
SELECT
RUNID,
RUNTYPEID,
GID,
duration,
EXTRACT(
YEAR
FROM
submissionDate
) AS yr,
EXTRACT(
MONTH
FROM
submissionDate
) AS mo,
submissionDate,
isEmulated,
EID,
regionName,
PID
FROM
Run;
CREATE VIEW AvgMonthlyRunTimePerGameCatPerYear AS
SELECT
DISTINCT GID,
-- regionName,
runTypeId,
yr,
CAST(SUM(duration) AS FLOAT) / 12 AS avgMonthlyRunTime
FROM
YearMonthRun
GROUP BY
GID,
-- regionName,
runTypeId,
yr
ORDER BY
GID,
-- regionName,
runTypeId,
yr;
SELECT
*
FROM
AvgMonthlyRunTimePerGameCatPerYear;
CREATE VIEW AllYears AS
SELECT
generate_series AS yr
FROM
generate_series(
(
SELECT
min(yr) :: INT
FROM
YearMonthRun
),
(
SELECT
max(yr) :: INT
FROM
YearMonthRun
)
);
CREATE VIEW AllItemsAllYears AS
SELECT
DISTINCT GID,
runTypeId,
ay.yr
FROM
(
SELECT
GID,
runTypeId
FROM
Run
) g,
AllYears ay;
CREATE VIEW AllAvgMonthlyRunTimes AS
SELECT
DISTINCT aiay.gid,
aiay.runTypeId,
aiay.yr,
COALESCE(avgMonthlyRunTime, 0.0) AS avgMonthlyRunTime
FROM
AllItemsAllYears aiay
LEFT JOIN AvgMonthlyRunTimePerGameCatPerYear iams ON (
iams.gid = aiay.gid
AND iams.runTypeId = aiay.runTypeId
AND iams.yr = aiay.yr
)
ORDER BY
aiay.yr;
-- recall that the primary key is (gid, regionName)
CREATE VIEW GameIds AS
SELECT
DISTINCT gid,
gameName
FROM
Game;
-- the change in yearly average between average monthly run times
-- of a game category
CREATE VIEW GameAndCategoryYearOverYearChange AS
SELECT
gameName,
runTypeName,
year1,
year1Average,
year2,
year2Average,
yearOverYearChange
FROM
(
SELECT
DISTINCT a.gid,
a.runTypeId,
a.yr AS Year1,
a.avgMonthlyRunTime AS Year1Average,
b.yr AS Year2,
b.avgMonthlyRunTime AS Year2Average,
(
CASE
WHEN a.avgMonthlyRunTime = 0
AND b.avgMonthlyRunTime != 0 THEN 'Infinity' :: FLOAT
WHEN a.avgMonthlyRunTime = b.avgMonthlyRunTime THEN 0.0 :: FLOAT
ELSE CAST(
(
(b.avgMonthlyRunTime - a.avgMonthlyRunTime) / a.avgMonthlyRunTime * 100
) AS FLOAT
)
END
) AS yearOverYearChange
FROM
AllAvgMonthlyRunTimes a
JOIN AllAvgMonthlyRunTimes b ON (
a.gid = b.gid
AND a.runTypeId = b.runTypeId
AND a.yr = b.yr - 1
)
) z
JOIN RunType rt ON z.runTypeId = rt.runTypeId
JOIN GameIds g ON z.gid = g.gid
ORDER BY
year1,
gameName,
yearOverYearChange;
SELECT
*
FROM
GameAndCategoryYearOverYearChange
WHERE
year2average != 0
AND NOT (
year1average = 0
AND year2average = 0
AND yearoveryearchange = 0
)
ORDER BY
yearOverYearChange;
-- Ignore where the game wasn't played in the first year
CREATE VIEW FilteredYOYChange AS
SELECT
*
FROM
GameAndCategoryYearOverYearChange
WHERE
year1average != 0
AND year2average != 0
AND NOT (
year1average = 0
AND year2average = 0
AND yearoveryearchange = 0
)
ORDER BY
yearOverYearChange;
-- you need to put this side by side with popularity year over year
CREATE VIEW AverageChangePerYearInterval AS
SELECT
year1,
year2,
COUNT(*) AS cnt,
AVG(yearoveryearchange) AS avg
FROM
FilteredYOYChange
GROUP BY
year1,
year2
ORDER BY
year1;
SELECT
year1,
year2,
COUNT(*) AS numYearIntervals,
COUNT(*) / (
SELECT
SUM(cnt)
FROM
AverageChangePerYearInterval
) AS weightOrNumYearIntervalsOutOfTotal,
AVG(yearoveryearchange) AS avgYearOverYearChange,
AVG(yearoveryearchange) * (
COUNT(*) / (
SELECT
SUM(cnt)
FROM
AverageChangePerYearInterval
)
) AS weightedAvgYearOverYearChange
FROM
FilteredYOYChange
GROUP BY
year1,
year2
ORDER BY
year1;
--------------------------------------------------------------------------------
-- How many attempts on average does it take to get a world record?
-- NOTE: limitation is that the number one people currently are the world record holders
CREATE VIEW Leaderboard AS
SELECT
DISTINCT t.runid,
p.pid,
p.playerName,
g.gameName,
g.gid,
t.regionName,
t.runTypeId,
t.duration,
t.submissionDate,
t.rnk
FROM
(
SELECT
runid,
pid,
gid,
regionName,
runTypeId,
duration,
submissionDate,
RANK() OVER (
PARTITION BY gid,
regionName,
runTypeId
ORDER BY
duration
) AS rnk
FROM
Run
) t
JOIN Player p ON t.pid = p.pid
JOIN Game g ON t.gid = g.gid
ORDER BY
gameName,
regionName,
runTypeId,
rnk;
-- get all the number one people
CREATE VIEW RankOnes AS
SELECT
*
FROM
Leaderboard
WHERE
rnk = 1;
CREATE VIEW NumAttemptsBeforeRankOne AS
SELECT
l.pid,
l.gameName,
l.regionName,
l.runTypeId,
COUNT(*) AS cnt
FROM
RankOnes r
JOIN Leaderboard l ON (
r.pid = l.pid
AND r.gid = l.gid
AND r.regionname = l.regionname
AND r.runtypeid = l.runtypeid
AND l.submissiondate < r.submissiondate
AND r.runid != l.runid
)
GROUP BY
l.pid,
l.gameName,
l.regionname,
l.runtypeid
ORDER BY
l.pid;
-- This is lower than we expect because these are runs that speedrunners
-- are willing to submit, _NOT_ all the runs they've ever done.
-- Naturally, this is a limitation because it depends on the runner
-- if they want to submit the speed run or not.
CREATE VIEW AverageNumAttemptsBeforeRankOne AS
SELECT
AVG(cnt) AS avgNumAttempsBeforeRankOneNum
FROM
NumAttemptsBeforeRankOne;