Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions dbt_project.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct, we want the the project_name to be relevant to the subject area, and we want to build the staging layer as views and the marts layer as tables. Nice work.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Name your project! Project names should contain only lowercase characters
# and underscores. A good package name should reflect your organization's
# name or the intended use of these models
name: 'my_new_project'
name: 'jaffle_shop'
version: '1.0.0'
config-version: 2

Expand Down Expand Up @@ -32,7 +32,13 @@ clean-targets: # directories to be removed by `dbt clean`
# as tables. These settings can be overridden in the individual model files
# using the `{{ config(...) }}` macro.
models:
my_new_project:
jaffle_shop:
# Applies to all files under models/example/
example:
materialized: view

staging:
materialized: view

marts:
materialized: table
28 changes: 16 additions & 12 deletions models/example/my_first_dbt_model.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This model could be removed from the project as it's really just a placeholder for the project.

Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@

/*
Welcome to your first dbt model!
Did you know that you can also configure models directly within SQL files?
This will override configurations stated in dbt_project.yml

Try changing "table" to "view" below
*/
{{ config(materialized="table") }}

{{ config(materialized='table') }}

with source_data as (
with
source_data as (

select 1 as id
union all
select null as id
select 1 as id, 'Jaggi' as fname, 'Krishna' as lname
union all
select 2 as id, 'Todd' as fname, 'Graham' as lname
union all
select 3 as id, 'Penny' as fname, 'Davis' as lname
union all
select null as id, 'Kelly' as fname, 'Greer' as lname

)
)

select *
from source_data
from
source_data

/*
/*
Uncomment the line below to remove records with null `id` values
*/

-- where id is not null
-- where id is not null
6 changes: 1 addition & 5 deletions models/example/my_second_dbt_model.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This model could be removed from the project as it's really just a placeholder for the project.

Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@

-- Use the `ref` function to select from other models

select *
from {{ ref('my_first_dbt_model') }}
where id = 1
select * from {{ ref("my_first_dbt_model") }} where id = 2
52 changes: 35 additions & 17 deletions models/example/schema.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@

version: 2

models:
- name: my_first_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
- name: my_first_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null

- name: FName
description: "The First Name of the Student"
tests:
- not_null

- name: LName
description: "The Last Name of the Student"
tests:
- not_null

- name: my_second_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
- name: FName
description: "The First Name of the Student"
tests:
- not_null

- name: my_second_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
- name: LName
description: "The Last Name of the Student"
tests:
- not_null
24 changes: 24 additions & 0 deletions models/example/source_data_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{ config(materialized="view") }}

with
source_data_view as (

select 1 as id, 'Jaggi' as fname, 'Krishna' as lname
union all
select 2 as id, 'Todd' as fname, 'Graham' as lname
union all
select 3 as id, 'Penny' as fname, 'Davis' as lname
union all
select 4 as id, 'John' as fname, 'Kiwa' as lname
union all
select null as id, 'Kelly' as fname, 'Greer' as lname

)

select *
from source_data_view

/*
Uncomment the line below to remove records with null `id` values
*/
where id is not null
31 changes: 31 additions & 0 deletions models/marts/finance/fct_orders.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, nice use of ref and I like how it pulls all of the components together for a final model.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
with orders as (
select * from {{ ref ('stg_jaffle_shop__orders' )}}
),

payments as (
select * from {{ ref ('stg_stripe__payments') }}
),

order_payments as (
select
order_id,
sum (case when status = 'success' then amount end) as amount

from payments
group by 1
),


final as (

select
orders.order_id,
orders.customer_id,
orders.order_date,
coalesce (order_payments.amount, 0) as amount

from orders
left join order_payments using (order_id)
)

select * from final
35 changes: 35 additions & 0 deletions models/marts/finance/fct_orders_jk1.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to see the enhancements in the code!

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
with orders as (
select * from {{ ref ('stg_jaffle_shop__orders' )}}
),

payments as (
select * from {{ ref ('stg_stripe__payments') }}
),


order_payments as (
select
order_id,
payments.status as payment_status,
sum (case when status = 'success' then amount * 100 end) as amount,
sum (case when status = 'fail' then amount * 100 end) as amount_failed,
sum (case when status is null then 0 else amount * 100 end) as amount_total
from payments
group by 1,2
),

final as (

select
orders.order_id,
orders.customer_id,
orders.order_date,
coalesce (order_payments.amount, 0) as amount,
order_payments.payment_status as payment_status1
from orders
left join order_payments using (order_id)
)

select * from final order by 1 asc


45 changes: 45 additions & 0 deletions models/marts/marketing/dim_customers.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, well formatted, proper use of the ref function. Nice work!

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
with customers as (

select * from {{ ref('stg_jaffle_shop__customers') }}

),

orders as (

select * from {{ ref('stg_jaffle_shop__orders') }}

),

customer_orders as (

select
customer_id,

min(order_date) as first_order_date,
max(order_date) as most_recent_order_date,
count(order_id) as number_of_orders

from orders

group by 1

),

final as (

select
customers.customer_id,
customers.first_name,
customers.last_name,
customer_orders.first_order_date,
customer_orders.most_recent_order_date,
coalesce (customer_orders.number_of_orders, 0)
as number_of_orders

from customers

left join customer_orders using (customer_id)

)

select * from final
19 changes: 19 additions & 0 deletions models/staging/jaffle_shop/create_customer_tbl.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can likely be removed as dbt will handle the table creation pieces

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

CREATE TABLE DBT_TRAINING.DBT_JKAVURU.customers
(
id NUMBER(9) PRIMARY KEY,
first_name VARCHAR(10) NOT NULL,
last_name VARCHAR(10) NOT NULL

);




/*

select id as customer_id, first_name, last_name

from DBT_TRAINING.DBT_JKAVURU.customers

*/
15 changes: 15 additions & 0 deletions models/staging/jaffle_shop/jaffle_shop__sources.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work on the sources config, looks like you the freshness may have been confusing, typically our sources are updated on a regular basis. The training data is static so it makes the source freshness hard to test.

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2

sources:
- name: jaffle_shop
database: raw
schema: jaffle_shop
freshness: # make this a little more strict
warn_after: {count: 24, period: hour}
error_after: {count: 1000, period: day} #Jaggi > increased from 12 to 9200 #
loaded_at_field: _etl_loaded_at

tables:
- name: customers
freshness: null #Jaggi, changed the freshness to null for customers model.
- name: orders
16 changes: 16 additions & 0 deletions models/staging/jaffle_shop/stg_jaffle_shop__customers.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice use of the source function. We could probably remove the commented block if this were not just training.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
select id as customer_id, first_name, last_name
from DBT_TRAINING.DBT_JKAVURU.customers

*/


select
id as customer_id,
first_name,
last_name
from {{ source('jaffle_shop', 'customers')}}

/*
from raw.jaffle_shop.customers
*/
20 changes: 20 additions & 0 deletions models/staging/jaffle_shop/stg_jaffle_shop__orders.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice use of the source function. We could probably remove the commented block if this were not just training.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice work!

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
select
id as order_id,
user_id as customer_id,
order_date,
status

from {{ source('jaffle_shop', 'orders') }}

/* raw.jaffle_shop.orders */

/*
select
id as order_id,
user_id as customer_id,
order_date,
status

from DBT_TRAINING.DBT_JKAVURU.orders

*/
11 changes: 11 additions & 0 deletions models/staging/stripe/stg_stripe__payments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
select
id as payment_id,
orderid as order_id,
paymentmethod as payment_method,
status,

-- amount is stored in cents, convert it to dollars
amount / 100 as amount,
created as created_at

from raw.stripe.payment