DatabaseCreation: sponsor_generator.sql

File sponsor_generator.sql, 5.3 KB (added by 231091, 5 hours ago)
Line 
1-- random sponsor names
2INSERT INTO
3 Sponsor (company_name)
4VALUES
5 ('Albatross Energy'),
6 ('Kronos Bank'),
7 ('Veltex Sportswear'),
8 ('Orion Telecom'),
9 ('Nexus Brewing Co.'),
10 ('Solara Motors'),
11 ('Pinnacle Insurance'),
12 ('Helix Pharma'),
13 ('Titan Construction'),
14 ('Ember Airlines'),
15 ('Cascade Finance'),
16 ('Polaris Media Group'),
17 ('Axiom Logistics'),
18 ('Verdant Foods'),
19 ('Stratos Tech'),
20 ('Cobalt Gaming'),
21 ('Meridian Hotels'),
22 ('Fenix Apparel'),
23 ('Crestwood Beverages'),
24 ('Aurum Jewellers'),
25 ('Driftwood Publishing'),
26 ('Ironclad Security'),
27 ('Luminary Cosmetics'),
28 ('Quantum Analytics');
29
30-- sponsor deals
31WITH
32 -- 1: find every (team, season) pair that exists in the data
33-- by unioning home and away teams from the Match table
34-- example:
35-- team_id season_id
36-- 1 10
37-- 2 10
38-- 1 11
39team_seasons AS (
40 SELECT
41 DISTINCT home_team_id AS team_id,
42 season_id
43 FROM
44 Match
45 UNION
46 SELECT
47 DISTINCT away_team_id AS team_id,
48 season_id
49 FROM
50 Match
51),
52-- 2: get the first and last match date for each season
53-- so we know the date range to use for the sponsor deal
54-- example:
55-- season_id season_start season_end
56-- 10 2023-08-12 2024-05-19
57-- 11 2024-08-10 2025-05-18
58season_date_ranges AS (
59 SELECT
60 season_id,
61 MIN(match_date) AS season_start,
62 MAX(match_date) AS season_end
63 FROM
64 Match
65 GROUP BY
66 season_id
67),
68-- 3: join team_seasons with season_date_ranges and randomly
69-- assign each (team, season) between 1 and 3 sponsors
70-- example:
71-- team_id season_id season_start season_end num_sponsors
72-- 1 10 2023-08-12 2024-05-19 2
73-- 2 10 2023-08-12 2024-05-19 1
74-- 3 10 2023-08-12 2024-05-19 3
75sponsor_assignments AS (
76 SELECT
77 ts.team_id,
78 ts.season_id,
79 sdr.season_start,
80 sdr.season_end,
81 1 + FLOOR(RANDOM() * 3) :: INT AS num_sponsors
82 FROM
83 team_seasons ts
84 JOIN season_date_ranges sdr ON sdr.season_id = ts.season_id
85),
86-- 4: cross join every (team, season) with every sponsor to get
87-- all possible pairings, then shuffle them randomly within each
88-- (team, season) group using ROW_NUMBER() + ORDER BY RANDOM()
89-- this gives each sponsor a unique random rank per (team, season)
90-- example:
91-- team_id season_id num_sponsors sponsor_id rn
92-- 1 10 2 3 1
93-- 1 10 2 1 2
94-- 1 10 2 4 3
95-- 1 10 2 2 4
96-- 2 10 1 2 1
97-- 2 10 1 4 2
98shuffled_sponsors AS (
99 SELECT
100 sa.team_id,
101 sa.season_id,
102 sa.season_start,
103 sa.season_end,
104 sa.num_sponsors,
105 sp.sponsor_id,
106 ROW_NUMBER() OVER (
107 PARTITION BY sa.team_id,
108 sa.season_id
109 ORDER BY
110 RANDOM()
111 ) AS rn
112 FROM
113 sponsor_assignments sa
114 CROSS JOIN Sponsor sp
115),
116-- 5: keep only the first N sponsors for each (team, season)
117-- where N is num_sponsors. because rn is unique within each group,
118-- this guarantees no duplicate sponsors for the same (team, season)
119-- example:
120-- team_id season_id season_start season_end sponsor_id
121-- 1 10 2023-08-12 2024-05-19 3
122-- 1 10 2023-08-12 2024-05-19 1
123-- 2 10 2023-08-12 2024-05-19 2
124matched_sponsors AS (
125 SELECT
126 team_id,
127 season_id,
128 season_start,
129 season_end,
130 sponsor_id
131 FROM
132 shuffled_sponsors
133 WHERE
134 rn <= num_sponsors
135),
136-- 6: assign a random deal value and random start/end dates
137-- that fall within the season's date range.
138-- start_date is a random date in the first 75% of the season,
139-- end_date is a random date between start_date and season_end.
140-- example:
141-- sponsor_id team_id season_id start_date end_date deal_value
142-- 3 1 10 2023-09-15 2024-02-28 12,340,000
143-- 1 1 10 2023-10-01 2024-04-10 4,780,000
144-- 2 2 10 2023-08-20 2023-12-05 890,000
145final_deals AS (
146 SELECT
147 sponsor_id,
148 team_id,
149 season_id,
150 ROUND((100000 + RANDOM() * 49900000) :: NUMERIC, -4) AS deal_value,
151 season_start + (RANDOM() * 0.75 * (season_end - season_start)) :: INT AS start_date,
152 season_start + (RANDOM() * (season_end - season_start)) :: INT AS end_date
153 FROM
154 matched_sponsors
155)
156-- 7: insert into Sponsor_deal, ensuring end_date is always
157-- after start_date by using GREATEST/LEAST to swap if needed
158INSERT INTO
159 Sponsor_deal (
160 sponsor_id,
161 team_id,
162 season_id,
163 deal_value,
164 start_date,
165 end_date
166 )
167SELECT sponsor_id,
168 team_id,
169 season_id,
170 deal_value,
171 LEAST(start_date, end_date) AS start_date,
172 GREATEST(start_date, end_date) + 30 AS end_date
173FROM
174 final_deals;