Skip to content

Commit e87b812

Browse files
authored
Merge pull request #4 from Shanabunga/add-warehouse
Add Warehouse Models - Star Schema
2 parents a8b499b + a3d03e6 commit e87b812

9 files changed

Lines changed: 298 additions & 0 deletions

File tree

dbt_project.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,6 @@ models:
3535
staging:
3636
+materialized: view
3737
+schema: staging
38+
warehouse:
39+
+materialized: table
40+
+schema: warehouse
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
with
2+
3+
calendar_dates as (
4+
5+
{{ dbt_utils.date_spine(
6+
datepart="day",
7+
start_date="cast('2018-01-01' as date)",
8+
end_date="cast('2050-12-31' as date)"
9+
)
10+
}}
11+
12+
),
13+
14+
final as (
15+
16+
select
17+
-- Surrogate Key
18+
{{ dbt_utils.generate_surrogate_key(
19+
['date(date_day)']
20+
)}} as calendar_date_sk,
21+
22+
-- Descriptive Values
23+
date_day::date as date_day,
24+
extract(isodow from date_day) as day_of_week_number,
25+
extract(day from date_day) as day_of_month_number,
26+
extract(doy from date_day) as day_of_year_number,
27+
28+
extract(week from date_day) as week_of_year_number,
29+
extract(month from date_day) as month_of_year_number,
30+
extract(quarter from date_day) as quarter_of_year_number,
31+
extract(year from date_day) as year_number,
32+
33+
to_char(date_day, 'Dy') as short_weekday_name,
34+
to_char(date_day, 'Day') as full_weekday_name,
35+
to_char(date_day, 'Mon') as short_month_name,
36+
to_char(date_day, 'Month') as full_month_name,
37+
38+
concat(to_char(date_day, 'Mon'), ' ', extract(year from date_day)) as short_month_year,
39+
40+
concat(to_char(date_day, 'Month'), ' ', extract(year from date_day)) as full_month_year
41+
42+
from calendar_dates
43+
44+
)
45+
46+
select * from final
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
with
2+
3+
games as (
4+
select * from {{ ref('stg_nba__games') }}
5+
),
6+
7+
final as (
8+
9+
select
10+
-- Surrogate Key
11+
{{ dbt_utils.generate_surrogate_key(
12+
['game_id']
13+
)}} as game_sk,
14+
15+
-- Descriptive Values
16+
game_id,
17+
home_team_id,
18+
home_team_name,
19+
away_team_id,
20+
away_team_name,
21+
league_id,
22+
league_name,
23+
season,
24+
25+
status_long,
26+
status_short,
27+
country_id,
28+
country_code,
29+
country_name,
30+
timezone,
31+
32+
-- Booleans
33+
is_home_team_win,
34+
is_away_team_win,
35+
36+
-- Dates
37+
game_date,
38+
game_time,
39+
game_at,
40+
created_at
41+
42+
/*
43+
-- DW Values
44+
null as dw_is_active,
45+
null as dw_created_at,
46+
null as dw_updated_at,
47+
null as dw_is_deleted,
48+
null as dw_deleted_at
49+
*/
50+
51+
from games
52+
53+
)
54+
55+
select * from final
56+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
with
2+
3+
teams as (
4+
select * from {{ ref('stg_nba__teams') }}
5+
),
6+
7+
franchises as (
8+
select * from {{ ref('stg_gsheets__franchise_actives') }}
9+
),
10+
11+
managers as (
12+
select * from {{ ref('stg_gsheets__franchise_general_managers') }}
13+
),
14+
15+
coaches as (
16+
select * from {{ ref('stg_gsheets__franchise_head_coaches') }}
17+
),
18+
19+
final as (
20+
21+
select
22+
-- Surrogate Key
23+
{{ dbt_utils.generate_surrogate_key(
24+
['teams.team_id',
25+
'franchises.team_id',
26+
'coaches.head_coach_id',
27+
'managers.general_manager_id']
28+
)}} as team_sk,
29+
30+
-- Descriptive Values
31+
teams.team_id,
32+
teams.team_name,
33+
teams.team_logo_url,
34+
teams.country_name,
35+
franchises.year_started,
36+
coaches.head_coach,
37+
managers.general_manager,
38+
managers.division,
39+
managers.conference
40+
41+
/*
42+
-- DW Values
43+
null as dw_is_active,
44+
null as dw_created_at,
45+
null as dw_updated_at,
46+
null as dw_is_deleted,
47+
null as dw_deleted_at
48+
*/
49+
50+
from teams
51+
52+
left join franchises
53+
on teams.team_name = franchises.franchise_name
54+
and franchises.is_current
55+
56+
left join managers
57+
on teams.team_name = managers.team_name
58+
59+
left join coaches
60+
on teams.team_name = coaches.team_name
61+
62+
)
63+
64+
select * from final
65+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
version: 2
2+
3+
models:
4+
- name: dim_calendar_dates
5+
columns:
6+
- name: calendar_date_sk
7+
data_tests:
8+
- unique
9+
- not_null
10+
- name: dim_games
11+
columns:
12+
- name: game_sk
13+
data_tests:
14+
- unique
15+
- not_null
16+
- name: dim_teams
17+
columns:
18+
- name: team_sk
19+
data_tests:
20+
- unique
21+
- not_null
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
version: 2
2+
3+
models:
4+
- name: fct_games_played
5+
columns:
6+
- name: fct_game_played_sk
7+
data_tests:
8+
- unique
9+
- not_null
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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

package-lock.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
packages:
2+
- package: dbt-labs/dbt_utils
3+
version: 1.1.1
4+
sha1_hash: a158c48c59c2bb7d729d2a4e215aabe5bb4f3353

packages.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
packages:
2+
- package: dbt-labs/dbt_utils
3+
version: 1.1.1

0 commit comments

Comments
 (0)