Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding the three new models #127

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .user.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
id: 8b0c8592-d3e0-4e1b-87a7-768e525254e5
5 changes: 3 additions & 2 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require-dbt-version: [">=1.0.0", "<2.0.0"]

models:
jaffle_shop:
materialized: table
staging:
materialized: view
+materialized: view
marts:
+materialized: table
19 changes: 19 additions & 0 deletions models/marts/core/int_orders_pivoted.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{%- set status_list = ['completed', 'shipped', 'returned', 'return_pending', 'placed'] -%}

with orders as (
select * from {{ ref('stg_orders') }}
),

pivoted as (
select
order_id,
{% for value in status_list -%}
(case when status = '{{ value }}' then 1 else 0 end) as {{ value }}_status
{%- if not loop.last -%}
,
{%- endif %}
{% endfor %}
from orders
)

select * from pivoted
31 changes: 31 additions & 0 deletions models/marts/finance/fct_orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
with orders as (
select * from {{ ref ('stg_orders') }}
),

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

order_payments as (
select
order_id,
sum (case when orders.status = 'completed' then payments.amount end) as amount

from orders
left join payments using (order_id)
group by 1
),

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

from orders
left join order_payments using (order_id)
)

select * from final
33 changes: 33 additions & 0 deletions models/marts/marketing/dim_customers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
with customers as (
select * from {{ ref ('stg_customers')}}
),

orders as (
select * from {{ ref ('fct_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,
sum(amount) as lifetime_value
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,
customer_orders.lifetime_value
from customers
left join customer_orders using (customer_id)
)

select * from final
57 changes: 57 additions & 0 deletions models/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,60 @@ models:
description: Amount of the order (AUD) paid for by gift card
tests:
- not_null

- name: dim_customers
description: This table has basic information about a customer, as well as some derived facts based on a customer's orders

columns:
- name: customer_id
description: This is a unique identifier for a customer
tests:
- unique
- not_null

- name: first_name
description: Customer's first name. PII.

- name: last_name
description: Customer's last name. PII.

- name: first_order
description: Date (UTC) of a customer's first order

- name: most_recent_order
description: Date (UTC) of a customer's most recent order

- name: number_of_orders
description: Count of the number of orders a customer has placed

- name: lifetime_value
description: Total value (AUD) of a customer's orders

- name: fct_orders
description: This table has basic information about orders, as well as some derived facts based on payments

columns:
- name: order_id
tests:
- unique
- not_null
description: This is a unique identifier for an order

- name: customer_id
description: Foreign key to the customers table
tests:
- not_null
- relationships:
to: ref('dim_customers')
field: customer_id

- name: order_date
description: Date (UTC) that the order was placed

- name: amount
description: Total amount (AUD) of the order
tests:
- not_null

- name: int_orders_pivoted
description: This table has basic information about orders, as well as some derived facts based on payments
12 changes: 12 additions & 0 deletions profiles.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
jaffle_shop:
target: dev
outputs:
dev:
type: postgres
host: localhost
user: postgres
password: '2020'
port: 5432
dbname: jaffle_shop
schema: dbt_cngue
threads: 4