Skip to content

Commit be860b1

Browse files
author
Student
committed
add staging models
1 parent 0e4af9d commit be860b1

5 files changed

Lines changed: 190 additions & 0 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
with
2+
3+
source_table as (
4+
select * from {{ source('franchises', 'actives') }}
5+
),
6+
7+
final as (
8+
9+
select
10+
TEAM_ID::int as team_id,
11+
FRANCHISE_ID::int as franchise_id,
12+
FRANCHISE_NAME as franchise_name,
13+
URL as url,
14+
"start"::int as year_started,
15+
"end"::int as year_ended,
16+
YEARS::int as years_active,
17+
GP::int as games_played,
18+
WINS::int as wins,
19+
LOSSES::int as losses,
20+
WIN::numeric / 100 as winning_percentage,
21+
PO::int as playoffs_made,
22+
DIV__TITLES::int as division_titles,
23+
CONF__TITLES::int as conference_titles,
24+
LEAGUE_TITLES::int as league_titles,
25+
"end" is null as is_current
26+
27+
from source_table
28+
29+
)
30+
31+
select * from final
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
with
2+
3+
source_table as (
4+
select * from {{ source('franchises', 'general_managers') }}
5+
),
6+
7+
final as (
8+
9+
select
10+
ID as general_manager_id,
11+
TEAM as team_name,
12+
COLLEGE as college_name,
13+
DIVISION as division,
14+
CONFERENCE as conference,
15+
16+
case
17+
when GENERAL_MANAGER = 'Vacant'
18+
then null
19+
else GENERAL_MANAGER
20+
end as general_manager,
21+
22+
PROFESSIONAL_CAREER as professional_career,
23+
YEAR_HIRED::int as year_hired,
24+
AS_OF_DATE::date as as_of_date,
25+
26+
date_part('year', AS_OF_DATE::date) - YEAR_HIRED::int as years_active
27+
28+
from source_table
29+
30+
)
31+
32+
select * from final
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
with
2+
3+
source_table as (
4+
select * from {{ source('franchises', 'head_coaches') }}
5+
),
6+
7+
final as (
8+
9+
select
10+
ID as head_coach_id,
11+
HEAD_COACH as head_coach,
12+
TEAM as team_name,
13+
DIVISION as division,
14+
CONFERENCE as conference,
15+
START_DATE::date as start_date,
16+
AS_OF_DATE::date as as_of_date,
17+
date_part('year', AS_OF_DATE::date) - date_part('year', START_DATE::date) as years_active
18+
19+
from source_table
20+
21+
)
22+
23+
select * from final
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
with
2+
3+
source_table as (
4+
select * from {{ source('nba', 'games') }}
5+
),
6+
7+
final as (
8+
9+
select
10+
id::integer as game_id,
11+
date::timestamp as game_at,
12+
date::date as game_date,
13+
time::time as game_time,
14+
15+
-- teams
16+
(teams::jsonb->'away'->>'id')::integer as away_team_id,
17+
teams::jsonb->'away'->>'name' as away_team_name,
18+
(teams::jsonb->'home'->>'id')::integer as home_team_id,
19+
teams::jsonb->'home'->>'name' as home_team_name,
20+
21+
-- league & season
22+
(league::jsonb->'id')::integer as league_id,
23+
replace((league::jsonb->'name')::text, '"','') as league_name,
24+
replace((league::jsonb->'season')::text, '"','') as season,
25+
26+
-- away team scoring
27+
(scores::jsonb->'away'->>'total')::integer as away_team_score_total,
28+
(scores::jsonb->'away'->>'quarter_1')::integer as away_team_score_quarter_1,
29+
(scores::jsonb->'away'->>'quarter_2')::integer as away_team_score_quarter_2,
30+
(scores::jsonb->'away'->>'quarter_3')::integer as away_team_score_quarter_3,
31+
(scores::jsonb->'away'->>'quarter_4')::integer as away_team_score_quarter_4,
32+
(coalesce(scores::jsonb->'away'->>'over_time', '0'))::integer as away_team_score_overtime,
33+
34+
-- home team scoring
35+
(scores::jsonb->'home'->>'total')::integer as home_team_score_total,
36+
(scores::jsonb->'home'->>'quarter_1')::integer as home_team_score_quarter_1,
37+
(scores::jsonb->'home'->>'quarter_2')::integer as home_team_score_quarter_2,
38+
(scores::jsonb->'home'->>'quarter_3')::integer as home_team_score_quarter_3,
39+
(scores::jsonb->'home'->>'quarter_4')::integer as home_team_score_quarter_4,
40+
(coalesce(scores::jsonb->'home'->>'over_time', '0'))::integer as home_team_score_overtime,
41+
42+
43+
-- Custom Metrics
44+
case
45+
when (scores::jsonb->'home'->>'total')::integer > (scores::jsonb->'away'->>'total')::integer
46+
then true
47+
else false
48+
end as is_home_team_win,
49+
50+
case
51+
when (scores::jsonb->'home'->>'total')::integer < (scores::jsonb->'away'->>'total')::integer
52+
then true
53+
else false
54+
end as is_away_team_win,
55+
56+
abs((scores::jsonb->'home'->>'total')::integer
57+
- (scores::jsonb->'away'->>'total')::integer) as point_differential,
58+
59+
60+
-- status
61+
replace((status::jsonb->'long')::text, '"','') as status_long,
62+
replace((status::jsonb->'short')::text, '"','') as status_short,
63+
64+
-- country
65+
(country::jsonb->'id')::integer as country_id,
66+
replace((country::jsonb->'code')::text, '"','') as country_code,
67+
replace((country::jsonb->'name')::text, '"','') as country_name,
68+
69+
timezone,
70+
to_timestamp(timestamp) as created_at,
71+
72+
row_number() over (partition by id order by _airbyte_emitted_at desc) = 1 is_latest
73+
74+
from source_table
75+
76+
)
77+
78+
select * from final where is_latest
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
with
2+
3+
source_table as (
4+
select * from {{ source('nba', 'teams') }}
5+
),
6+
7+
final as (
8+
9+
select
10+
id::integer as team_id,
11+
name as team_name,
12+
logo as team_logo_url,
13+
14+
case
15+
when name = 'Toronto Raptors'
16+
then 'Canada'
17+
else 'USA'
18+
end as country_name,
19+
20+
row_number() over (partition by id order by _airbyte_emitted_at desc) = 1 is_latest
21+
22+
from source_table
23+
24+
)
25+
26+
select * from final where is_latest

0 commit comments

Comments
 (0)