-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselects.sql
More file actions
200 lines (183 loc) · 5.23 KB
/
selects.sql
File metadata and controls
200 lines (183 loc) · 5.23 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
create view FinishedGames as
select * from Games
where EndTime is not null;
create or replace function GetAllTeamsPrizes()
returns table (
TeamName varchar(200),
TotalPrizes bigint
)
as
$$
begin
return query
select distinct Teams.TeamName as TeamName, q1.TotalPrize as TotalPrizes from Teams
natural join (
select TeamId, sum(Prize) as TotalPrize from Teams
natural join TeamTournaments
natural join TournamentPrizes
group by TeamId) q1;
end;
$$ language plpgsql;
create or replace function AvgGameDuration()
returns double precision
as
$$
declare
answer double precision;
begin
select avg(extract(epoch from (EndTime - StartTime)))
from FinishedGames
into answer;
return answer;
end;
$$ language plpgsql;
create or replace function AllPlayersOfTeam(TeamIdArg integer)
returns table (Username varchar(200))
as
$$
begin
return query
select Users.Username from Teams
natural join TeamsUsers
natural join Users
where TeamId = TeamIdArg;
end;
$$ language plpgsql;
create or replace function AllPlayersKD()
returns table (UserId integer,
KD double precision)
as
$$
begin
return query
select UserStat.UserId, (cast(sum(Kills) as double precision) / (cast(sum(Deaths) as double precision) + 1.0)) as KD
from UserStat
natural join FinishedGames
group by UserStat.UserId;
end;
$$ language plpgsql;
create or replace function AllPlayersKDLastNMatches(LastMatchesCount integer)
returns table (UserId integer,
KD double precision)
as
$$
begin
return query
select q3.UserId, (cast(sum(q3.Kills) as double precision) / (cast(sum(q3.Deaths) as double precision) + 1.0)) as KD
from (select q2.UserId, q2.Kills, q2.Deaths from
(select row_number() over (partition by q1.UserId order by q1.StartTime) as r, q1.*
from (select * from UserStat
natural join FinishedGames) q1
) q2
where q2.r <= LastMatchesCount
) q3
group by q3.UserId;
end;
$$ language plpgsql;
create or replace function AllPlayersKDInCertainPeriod(PeriodStart timestamp,
PeriodEnd timestamp)
returns table (UserId integer,
KD double precision)
as
$$
begin
return query
select q3.UserId, (cast(sum(q3.Kills) as double precision) / (cast(sum(q3.Deaths) as double precision) + 1.0)) as KD
from (
select us.UserId, us.Kills, us.Deaths
from UserStat us
natural join FinishedGames
where PeriodStart <= FinishedGames.StartTime and FinishedGames.StartTime <= PeriodEnd
) q3
group by q3.UserId;
end;
$$ language plpgsql;
create or replace function AllPlayersKDOnCertainMap(PlayedMapArg MapEnum)
returns table (UserId integer,
KD double precision)
as
$$
begin
return query
select q3.UserId, (cast(sum(q3.Kills) as double precision) / (cast(sum(q3.Deaths) as double precision) + 1.0)) as KD
from (
select us.UserId, us.Kills, us.Deaths
from UserStat us
natural join FinishedGames
where FinishedGames.PlayedMap = PlayedMapArg
) q3
group by q3.UserId;
end;
$$ language plpgsql;
create or replace function PlayerWinrate(PlayerId integer)
returns double precision
as
$$
declare
Wins double precision;
TotalGames integer;
begin
select count(GameId) from UserStat
where UserId = PlayerId
into TotalGames;
if TotalGames = 0
then
return 0.0;
end if;
select count(GameId) from (
select GameId from Users
natural join TeamsUsers
natural join Teams
inner join Matches on Teams.TeamId = Matches.PickingSideTeamId
natural join FinishedGames
where FinishedGames.WonRoundsTeam1 > FinishedGames.WonRoundsTeam2 and UserId = PlayerId
union
select GameId from Users
natural join TeamsUsers
natural join Teams
inner join Matches on Teams.TeamId = Matches.OtherTeam
natural join FinishedGames
where FinishedGames.WonRoundsTeam1 < FinishedGames.WonRoundsTeam2 and UserId = PlayerId) q1
into Wins;
return cast(Wins as double precision) / TotalGames * 100.0;
end;
$$ language plpgsql;
create or replace function PlayerLastResults(PlayerId integer)
returns table (GameId integer,
Res char(1))
as
$$
begin
return query
select q1.GameId, q1.Res
from (
(select FinishedGames.GameId, StartTime, cast('W' as char) as Res from Users
natural join TeamsUsers
natural join Teams
inner join Matches on Teams.TeamId = Matches.PickingSideTeamId
natural join FinishedGames
where FinishedGames.WonRoundsTeam1 > FinishedGames.WonRoundsTeam2 and UserId = PlayerId
union
select FinishedGames.GameId, StartTime, cast('W' as char) as Res from Users
natural join TeamsUsers
natural join Teams
inner join Matches on Teams.TeamId = Matches.OtherTeam
natural join FinishedGames
where FinishedGames.WonRoundsTeam1 < FinishedGames.WonRoundsTeam2 and UserId = PlayerId)
union
(select FinishedGames.GameId, StartTime, cast('L' as char) as Res from Users
natural join TeamsUsers
natural join Teams
inner join Matches on Teams.TeamId = Matches.PickingSideTeamId
natural join FinishedGames
where FinishedGames.WonRoundsTeam1 < FinishedGames.WonRoundsTeam2 and UserId = PlayerId
union
select FinishedGames.GameId, StartTime, cast('L' as char) as Res from Users
natural join TeamsUsers
natural join Teams
inner join Matches on Teams.TeamId = Matches.OtherTeam
natural join FinishedGames
where FinishedGames.WonRoundsTeam1 > FinishedGames.WonRoundsTeam2 and UserId = PlayerId)) q1
order by StartTime;
end;
$$ language plpgsql;