-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask061.pgsql
45 lines (40 loc) · 1009 Bytes
/
Task061.pgsql
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
create or replace function calculate1()
returns integer
language plpgsql
as $$
declare
tot_wins integer;
win integer;
winways integer;
pos integer;
ms integer;
dist integer;
times text[];
distances text[];
timeval text;
begin
select '{' || regexp_replace(trim(readings), ' +', ',', 'g') || '}' into times
from races
where unit = 'Time';
select '{' || regexp_replace(trim(readings), ' +', ',', 'g') || '}' into distances
from races
where unit = 'Distance';
winways := 1;
pos := 0;
foreach timeval in array times loop
tot_wins := 0;
pos := pos + 1;
ms := cast(timeval as integer);
dist := cast(distances[pos] as integer);
for i in 0..ms loop
win := i * (ms - i);
if (win > dist) then
tot_wins := tot_wins + 1;
end if;
end loop;
winways := winways * tot_wins;
end loop;
return winways;
end;
$$
select calculate1();