-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
69 changed files
with
1,570 additions
and
1,405 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,44 @@ | ||
WITH cte_playoffs_r1 AS ( | ||
SELECT | ||
winning_team, | ||
COUNT(*) AS made_playoffs | ||
FROM {{ ref( 'initialize_seeding' ) }} | ||
GROUP BY ALL | ||
), | ||
with | ||
cte_playoffs_r1 as ( | ||
select winning_team, count(*) as made_playoffs | ||
from {{ ref("initialize_seeding") }} | ||
group by all | ||
), | ||
|
||
cte_playoffs_r2 AS ( | ||
SELECT | ||
winning_team, | ||
COUNT(*) AS made_conf_semis | ||
FROM {{ ref( 'playoff_sim_r1' ) }} | ||
GROUP BY ALL | ||
), | ||
cte_playoffs_r2 as ( | ||
select winning_team, count(*) as made_conf_semis | ||
from {{ ref("playoff_sim_r1") }} | ||
group by all | ||
), | ||
|
||
cte_playoffs_r3 AS ( | ||
SELECT | ||
winning_team, | ||
COUNT(*) AS made_conf_finals | ||
FROM {{ ref( 'playoff_sim_r2' ) }} | ||
GROUP BY ALL | ||
), | ||
cte_playoffs_r3 as ( | ||
select winning_team, count(*) as made_conf_finals | ||
from {{ ref("playoff_sim_r2") }} | ||
group by all | ||
), | ||
|
||
cte_playoffs_r4 AS ( | ||
SELECT | ||
winning_team, | ||
COUNT(*) AS made_finals | ||
FROM {{ ref( 'playoff_sim_r3' ) }} | ||
GROUP BY ALL | ||
), | ||
cte_playoffs_r4 as ( | ||
select winning_team, count(*) as made_finals | ||
from {{ ref("playoff_sim_r3") }} | ||
group by all | ||
), | ||
|
||
cte_playoffs_finals AS ( | ||
SELECT | ||
winning_team, | ||
COUNT(*) AS won_finals | ||
FROM {{ ref( 'playoff_sim_r4' ) }} | ||
GROUP BY ALL | ||
) | ||
cte_playoffs_finals as ( | ||
select winning_team, count(*) as won_finals | ||
from {{ ref("playoff_sim_r4") }} | ||
group by all | ||
) | ||
|
||
SELECT | ||
T.team, | ||
R1.made_playoffs, | ||
R2.made_conf_semis, | ||
R3.made_conf_finals, | ||
R4.made_finals, | ||
F.won_finals | ||
FROM {{ ref( 'nba_teams' ) }} T | ||
LEFT JOIN cte_playoffs_r1 R1 ON R1.winning_team = T.team | ||
LEFT JOIN cte_playoffs_r2 R2 ON R2.winning_team = T.team | ||
LEFT JOIN cte_playoffs_r3 R3 ON R3.winning_team = T.team | ||
LEFT JOIN cte_playoffs_r4 R4 ON R4.winning_team = T.team | ||
LEFT JOIN cte_playoffs_finals F ON F.winning_team = T.team | ||
select | ||
t.team, | ||
r1.made_playoffs, | ||
r2.made_conf_semis, | ||
r3.made_conf_finals, | ||
r4.made_finals, | ||
f.won_finals | ||
from {{ ref("nba_teams") }} t | ||
left join cte_playoffs_r1 r1 on r1.winning_team = t.team | ||
left join cte_playoffs_r2 r2 on r2.winning_team = t.team | ||
left join cte_playoffs_r3 r3 on r3.winning_team = t.team | ||
left join cte_playoffs_r4 r4 on r4.winning_team = t.team | ||
left join cte_playoffs_finals f on f.winning_team = t.team |
190 changes: 91 additions & 99 deletions
190
transform/models/nba/analysis/reg_season_actuals_enriched.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,100 @@ | ||
{{ | ||
config( | ||
materialized='table' | ||
) | ||
}} | ||
|
||
WITH cte_wins AS ( | ||
SELECT | ||
winning_team, | ||
COUNT(*) as wins | ||
FROM {{ ref( 'nba_latest_results' ) }} | ||
GROUP BY ALL | ||
), | ||
{{ config(materialized="table") }} | ||
|
||
cte_losses AS ( | ||
SELECT | ||
losing_team, | ||
COUNT(*) as losses | ||
FROM {{ ref( 'nba_latest_results' ) }} | ||
GROUP BY ALL | ||
), | ||
with | ||
cte_wins as ( | ||
select winning_team, count(*) as wins | ||
from {{ ref("nba_latest_results") }} | ||
group by all | ||
), | ||
|
||
cte_losses as ( | ||
select losing_team, count(*) as losses | ||
from {{ ref("nba_latest_results") }} | ||
group by all | ||
), | ||
|
||
cte_favored_wins AS ( | ||
SELECT | ||
LR.winning_team, | ||
COUNT(*) as wins | ||
FROM {{ ref( 'nba_latest_results' ) }} LR | ||
INNER JOIN {{ ref( 'nba_results_log' ) }} R ON R.game_id = LR.game_id | ||
AND R.favored_team = LR.winning_team | ||
GROUP BY ALL | ||
), | ||
cte_favored_wins as ( | ||
select lr.winning_team, count(*) as wins | ||
from {{ ref("nba_latest_results") }} lr | ||
inner join | ||
{{ ref("nba_results_log") }} r | ||
on r.game_id = lr.game_id | ||
and r.favored_team = lr.winning_team | ||
group by all | ||
), | ||
|
||
cte_favored_losses AS ( | ||
SELECT | ||
LR.losing_team, | ||
COUNT(*) as losses | ||
FROM {{ ref( 'nba_latest_results' ) }} LR | ||
INNER JOIN {{ ref( 'nba_results_log' ) }} R ON R.game_id = LR.game_id | ||
AND R.favored_team = LR.losing_team | ||
GROUP BY ALL | ||
), | ||
cte_favored_losses as ( | ||
select lr.losing_team, count(*) as losses | ||
from {{ ref("nba_latest_results") }} lr | ||
inner join | ||
{{ ref("nba_results_log") }} r | ||
on r.game_id = lr.game_id | ||
and r.favored_team = lr.losing_team | ||
group by all | ||
), | ||
|
||
cte_avg_opponent_wins AS ( | ||
SELECT | ||
LR.winning_team, | ||
COUNT(*) as wins | ||
FROM {{ ref( 'nba_latest_results' ) }} LR | ||
INNER JOIN {{ ref( 'nba_results_log' ) }} R ON R.game_id = LR.game_id | ||
AND ( (LR.winning_team = R.home_team AND R.visiting_team_above_avg = 1) | ||
OR (LR.winning_team = R.visiting_team AND R.home_team_above_avg = 1) ) | ||
GROUP BY ALL | ||
), | ||
cte_avg_opponent_wins as ( | ||
select lr.winning_team, count(*) as wins | ||
from {{ ref("nba_latest_results") }} lr | ||
inner join | ||
{{ ref("nba_results_log") }} r | ||
on r.game_id = lr.game_id | ||
and ( | ||
(lr.winning_team = r.home_team and r.visiting_team_above_avg = 1) | ||
or (lr.winning_team = r.visiting_team and r.home_team_above_avg = 1) | ||
) | ||
group by all | ||
), | ||
|
||
cte_avg_opponent_losses AS ( | ||
SELECT | ||
LR.losing_team, | ||
COUNT(*) as losses | ||
FROM {{ ref( 'nba_latest_results' ) }} LR | ||
INNER JOIN {{ ref( 'nba_results_log' ) }} R ON R.game_id = LR.game_id | ||
AND ( (LR.losing_team = R.visiting_team AND R.home_team_above_avg = 1) | ||
OR (LR.losing_team = R.home_team AND R.visiting_team_above_avg = 1) ) | ||
GROUP BY ALL | ||
), | ||
cte_avg_opponent_losses as ( | ||
select lr.losing_team, count(*) as losses | ||
from {{ ref("nba_latest_results") }} lr | ||
inner join | ||
{{ ref("nba_results_log") }} r | ||
on r.game_id = lr.game_id | ||
and ( | ||
(lr.losing_team = r.visiting_team and r.home_team_above_avg = 1) | ||
or (lr.losing_team = r.home_team and r.visiting_team_above_avg = 1) | ||
) | ||
group by all | ||
), | ||
|
||
cte_home_wins AS ( | ||
SELECT | ||
LR.home_team, | ||
COUNT(*) as wins | ||
FROM {{ ref( 'nba_latest_results' ) }} LR | ||
WHERE LR.home_team = LR.winning_team | ||
GROUP BY ALL | ||
), | ||
cte_home_wins as ( | ||
select lr.home_team, count(*) as wins | ||
from {{ ref("nba_latest_results") }} lr | ||
where lr.home_team = lr.winning_team | ||
group by all | ||
), | ||
|
||
cte_home_losses AS ( | ||
SELECT | ||
LR.home_team, | ||
COUNT(*) as losses | ||
FROM {{ ref( 'nba_latest_results' ) }} LR | ||
WHERE LR.home_team = LR.losing_team | ||
GROUP BY ALL | ||
) | ||
cte_home_losses as ( | ||
select lr.home_team, count(*) as losses | ||
from {{ ref("nba_latest_results") }} lr | ||
where lr.home_team = lr.losing_team | ||
group by all | ||
) | ||
|
||
SELECT | ||
T.team, | ||
COALESCE(W.wins, 0) AS wins, | ||
COALESCE(L.losses, 0) AS losses, | ||
COALESCE(FW.wins, 0) AS wins_as_favorite, | ||
COALESCE(FL.losses, 0) AS losses_as_favorite, | ||
COALESCE(W.wins, 0) - COALESCE(FW.wins, 0) AS wins_as_underdog, | ||
COALESCE(L.losses, 0) - COALESCE(FL.losses, 0) AS losses_as_underdog, | ||
COALESCE(AW.wins,0) AS wins_vs_good_teams, | ||
COALESCE(AL.losses,0) AS losses_vs_good_teams, | ||
COALESCE(W.wins, 0) - COALESCE(AW.wins, 0) AS wins_vs_bad_teams, | ||
COALESCE(L.losses, 0) - COALESCE(AL.losses, 0) AS losses_vs_bad_teams, | ||
COALESCE(HW.wins,0) AS home_wins, | ||
COALESCE(HL.losses,0) AS home_losses, | ||
COALESCE(W.wins, 0) - COALESCE(HW.wins, 0) AS away_wins, | ||
COALESCE(L.losses, 0) - COALESCE(HL.losses, 0) AS away_losses | ||
FROM {{ ref( 'nba_teams' ) }} T | ||
LEFT JOIN cte_wins W ON W.winning_team = T.team_long | ||
LEFT JOIN cte_losses L ON L.losing_team = T.Team_long | ||
LEFT JOIN cte_favored_wins FW ON FW.winning_team = T.Team_long | ||
LEFT JOIN cte_favored_losses FL ON FL.losing_team = T.Team_long | ||
LEFT JOIN cte_avg_opponent_wins AW ON AW.winning_team = T.Team_long | ||
LEFT JOIN cte_avg_opponent_losses AL ON AL.losing_team = T.Team_long | ||
LEFT JOIN cte_home_wins HW ON HW.home_team = T.Team_long | ||
LEFT JOIN cte_home_losses HL ON HL.home_team = T.Team_long | ||
select | ||
t.team, | ||
coalesce(w.wins, 0) as wins, | ||
coalesce(l.losses, 0) as losses, | ||
coalesce(fw.wins, 0) as wins_as_favorite, | ||
coalesce(fl.losses, 0) as losses_as_favorite, | ||
coalesce(w.wins, 0) - coalesce(fw.wins, 0) as wins_as_underdog, | ||
coalesce(l.losses, 0) - coalesce(fl.losses, 0) as losses_as_underdog, | ||
coalesce(aw.wins, 0) as wins_vs_good_teams, | ||
coalesce(al.losses, 0) as losses_vs_good_teams, | ||
coalesce(w.wins, 0) - coalesce(aw.wins, 0) as wins_vs_bad_teams, | ||
coalesce(l.losses, 0) - coalesce(al.losses, 0) as losses_vs_bad_teams, | ||
coalesce(hw.wins, 0) as home_wins, | ||
coalesce(hl.losses, 0) as home_losses, | ||
coalesce(w.wins, 0) - coalesce(hw.wins, 0) as away_wins, | ||
coalesce(l.losses, 0) - coalesce(hl.losses, 0) as away_losses | ||
from {{ ref("nba_teams") }} t | ||
left join cte_wins w on w.winning_team = t.team_long | ||
left join cte_losses l on l.losing_team = t.team_long | ||
left join cte_favored_wins fw on fw.winning_team = t.team_long | ||
left join cte_favored_losses fl on fl.losing_team = t.team_long | ||
left join cte_avg_opponent_wins aw on aw.winning_team = t.team_long | ||
left join cte_avg_opponent_losses al on al.losing_team = t.team_long | ||
left join cte_home_wins hw on hw.home_team = t.team_long | ||
left join cte_home_losses hl on hl.home_team = t.team_long |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,41 @@ | ||
WITH cte_wins AS ( | ||
SELECT | ||
S.scenario_id, | ||
S.winning_team, | ||
CASE | ||
WHEN S.winning_team = S.home_team THEN S.home_conf | ||
ELSE S.visiting_conf | ||
END AS conf, | ||
/* CASE | ||
with | ||
cte_wins as ( | ||
select | ||
s.scenario_id, | ||
s.winning_team, | ||
case | ||
when s.winning_team = s.home_team then s.home_conf else s.visiting_conf | ||
end as conf, | ||
/* CASE | ||
WHEN S.winning_team = S.home_team THEN S.home_team_elo_rating | ||
ELSE S.visiting_team_elo_rating | ||
END AS elo_rating, */ | ||
COUNT(*) AS wins | ||
FROM {{ ref( 'reg_season_simulator' ) }} S | ||
GROUP BY ALL | ||
), | ||
count(*) as wins | ||
from {{ ref("reg_season_simulator") }} s | ||
group by all | ||
), | ||
|
||
cte_ranked_wins AS ( | ||
SELECT | ||
*, | ||
--no tiebreaker, so however row number handles order ties will need to be dealt with | ||
ROW_NUMBER() OVER (PARTITION BY scenario_id, conf ORDER BY wins DESC, winning_team DESC ) AS season_rank | ||
FROM cte_wins | ||
cte_ranked_wins as ( | ||
select | ||
*, | ||
-- no tiebreaker, so however row number handles order ties will need to be | ||
-- dealt with | ||
row_number() over ( | ||
partition by scenario_id, conf order by wins desc, winning_team desc | ||
) as season_rank | ||
from cte_wins | ||
|
||
), | ||
), | ||
|
||
cte_made_playoffs AS ( | ||
SELECT | ||
*, | ||
CASE | ||
WHEN season_rank <= 10 THEN 1 | ||
ELSE 0 | ||
END AS made_playoffs, | ||
CASE | ||
WHEN season_rank BETWEEN 7 AND 10 THEN 1 | ||
ELSE 0 | ||
END AS made_play_in, | ||
conf || '-' || season_rank::text AS seed | ||
FROM cte_ranked_wins | ||
) | ||
cte_made_playoffs as ( | ||
select | ||
*, | ||
case when season_rank <= 10 then 1 else 0 end as made_playoffs, | ||
case when season_rank between 7 and 10 then 1 else 0 end as made_play_in, | ||
conf || '-' || season_rank::text as seed | ||
from cte_ranked_wins | ||
) | ||
|
||
SELECT | ||
MP.*, | ||
LE.elo_rating, | ||
{{ var( 'sim_start_game_id' ) }} AS sim_start_game_id | ||
FROM cte_made_playoffs MP | ||
LEFT JOIN {{ ref( 'nba_latest_elo' ) }} LE ON LE.team = MP.winning_team | ||
select mp.*, le.elo_rating, {{ var("sim_start_game_id") }} as sim_start_game_id | ||
from cte_made_playoffs mp | ||
left join {{ ref("nba_latest_elo") }} le on le.team = mp.winning_team |
Oops, something went wrong.