-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcategorized_results_view.sql
More file actions
79 lines (77 loc) · 2.65 KB
/
categorized_results_view.sql
File metadata and controls
79 lines (77 loc) · 2.65 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
-- 1. Drop existing view if it exists (must specify if it was a standard view)
DROP MATERIALIZED VIEW IF EXISTS categorized_results;
-- 2. Create the Materialized View
CREATE MATERIALIZED VIEW categorized_results AS
WITH RankedResults AS (
SELECT
res.*,
r.name AS rider_name,
r.gender,
r.birth_year,
-- Rank individual races for the "Best of X" calculation
ROW_NUMBER() OVER (
PARTITION BY res.rider_id, res.year
ORDER BY res.points DESC
) as race_rank,
-- Define the "Best of X" limit for each year
CASE
WHEN res.year = '2025' THEN 4
WHEN res.year = '2024' THEN 4
WHEN res.year = '2023' THEN 3
ELSE 5
END as points_limit
FROM results res
JOIN riders r ON res.rider_id = r.id
),
AggregatedTotals AS (
SELECT
year,
rider_id,
rider_name,
gender,
year - birth_year AS age_at_race,
SUM(points) AS total_points,
-- Calculate season points based on the "Best of X"
SUM(CASE WHEN race_rank <= points_limit THEN points ELSE 0 END) AS season_points,
COUNT(race_id) AS total_races,
-- Category Label logic
CASE
WHEN (year - birth_year) < 20 THEN 'Under 20'
WHEN (year - birth_year) BETWEEN 20 AND 29 THEN '20-29'
WHEN (year - birth_year) BETWEEN 30 AND 39 THEN '30-39'
WHEN (year - birth_year) BETWEEN 40 AND 49 THEN '40-49'
WHEN (year - birth_year) BETWEEN 50 AND 59 THEN '50-59'
WHEN (year - birth_year) BETWEEN 60 AND 69 THEN '60-69'
WHEN (year - birth_year) BETWEEN 70 AND 74 THEN '70-74'
WHEN (year - birth_year) BETWEEN 75 AND 79 THEN '75-79'
WHEN (year - birth_year) >= 80 THEN '80+'
ELSE 'Unknown'
END || ' ' ||
CASE
WHEN gender = 'M' THEN 'Men'
WHEN gender = 'W' THEN 'Women'
ELSE ''
END AS category_label
FROM RankedResults
GROUP BY
year,
rider_id,
rider_name,
gender,
birth_year
)
SELECT
*,
-- Calculate Overall Standing Rank per year
RANK() OVER (
PARTITION BY year , gender
ORDER BY season_points DESC
) as overall_standing_rank,
-- Calculate Category Standing Rank per year
RANK() OVER (
PARTITION BY year, category_label
ORDER BY season_points DESC
) as category_standing_rank
FROM AggregatedTotals;
-- 3. Create an index on the materialized view for fast rider lookups
CREATE INDEX idx_categorized_results_rider_id ON categorized_results (rider_id);