|
| 1 | +with |
| 2 | + |
| 3 | +games as ( |
| 4 | + select * from {{ ref('stg_nba__games') }} |
| 5 | +), |
| 6 | + |
| 7 | +dim_games as ( |
| 8 | + select * from {{ ref('dim_games') }} |
| 9 | +), |
| 10 | + |
| 11 | +dim_teams as ( |
| 12 | + select * from {{ ref('dim_teams') }} |
| 13 | +), |
| 14 | + |
| 15 | +final as ( |
| 16 | + |
| 17 | + select |
| 18 | + -- Surrogate Key |
| 19 | + {{ dbt_utils.generate_surrogate_key( |
| 20 | + ['dim_games.game_sk', |
| 21 | + 'home_teams.team_sk', |
| 22 | + 'away_teams.team_sk', |
| 23 | + 'date(games.game_at)' |
| 24 | + ] |
| 25 | + )}} as fct_game_played_sk, |
| 26 | + |
| 27 | + -- Dimension Keys |
| 28 | + dim_games.game_sk, |
| 29 | + home_teams.team_sk as home_team_sk, |
| 30 | + away_teams.team_sk as away_team_sk, |
| 31 | + |
| 32 | + {{ dbt_utils.generate_surrogate_key( |
| 33 | + ['date(games.game_at)'] |
| 34 | + )}} as game_date_sk, |
| 35 | + |
| 36 | + -- Numeric Values |
| 37 | + games.home_team_score_total, |
| 38 | + games.home_team_score_quarter_1, |
| 39 | + games.home_team_score_quarter_2, |
| 40 | + games.home_team_score_quarter_3, |
| 41 | + games.home_team_score_quarter_4, |
| 42 | + games.home_team_score_overtime, |
| 43 | + round(games.home_team_score_total / 4, 2) as average_home_pts_per_quarter, |
| 44 | + |
| 45 | + games.away_team_score_total, |
| 46 | + games.away_team_score_quarter_1, |
| 47 | + games.away_team_score_quarter_2, |
| 48 | + games.away_team_score_quarter_3, |
| 49 | + games.away_team_score_quarter_4, |
| 50 | + games.away_team_score_overtime, |
| 51 | + round(games.home_team_score_total / 4, 2) as average_away_pts_per_quarter, |
| 52 | + |
| 53 | + games.point_differential, |
| 54 | + |
| 55 | + case |
| 56 | + when games.is_home_team_win then 1 |
| 57 | + else 0 |
| 58 | + end as home_team_win_count, |
| 59 | + |
| 60 | + case |
| 61 | + when not games.is_home_team_win then 1 |
| 62 | + else 0 |
| 63 | + end as home_team_loss_count, |
| 64 | + |
| 65 | + case |
| 66 | + when games.is_away_team_win then 1 |
| 67 | + else 0 |
| 68 | + end as away_team_win_count, |
| 69 | + |
| 70 | + case |
| 71 | + when not games.is_away_team_win then 1 |
| 72 | + else 0 |
| 73 | + end as away_team_loss_count, |
| 74 | + |
| 75 | + -- Dates (for simplicity) |
| 76 | + games.game_at |
| 77 | + |
| 78 | + from games |
| 79 | + |
| 80 | + left join dim_games |
| 81 | + on games.game_id = dim_games.game_id |
| 82 | + |
| 83 | + left join dim_teams as home_teams |
| 84 | + on games.home_team_id = home_teams.team_id |
| 85 | + |
| 86 | + left join dim_teams as away_teams |
| 87 | + on games.away_team_id = away_teams.team_id |
| 88 | + |
| 89 | +) |
| 90 | + |
| 91 | +select * from final |
0 commit comments