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

Add fct_order_items model #3

Open
wants to merge 5 commits into
base: master
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
38 changes: 38 additions & 0 deletions models/example/fct_order_items.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{#
create a fct_order_items table joining orders to order items.
select all fields from order items and join in the order_date,
email and customer_id from the orders table
#}
with orderitems as (

select * from {{ ref('order_items_upload') }}
Copy link
Collaborator

Choose a reason for hiding this comment

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

this should be ref the staging models that you built! stg_orders and stg_order_items


),

orders as (

eogilvy12 marked this conversation as resolved.
Show resolved Hide resolved
select * from {{ ref('orders_upload') }}

),

final as (

select
orderitems.id as order_item_id,
orderitems.order_id,
orderitems.price,
Copy link
Collaborator

Choose a reason for hiding this comment

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

it is not typical to list out explicit order_item fields since you have already done that in the stg model. typically, it will be select orderitems.*, orders.whichever fields. let me know if this makes sense!

orderitems.quantity,
orderitems.size,
orderitems.color,
orderitems.product_id,
orders.created_at,
orders.email,
orders.customer_id

from orderitems

inner join orders on orderitems.order_id = orders.id

)

select * from final
29 changes: 29 additions & 0 deletions models/example/order_calculations.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
with orders as (

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

),

orderitems as (
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think you use the order_items CTE anywhere in this model so you can remove


select * from {{ ref('stg_order_items') }}
),

calc as (

select
orders.customer_id,
Copy link
Collaborator

Choose a reason for hiding this comment

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

this is another situation where you want to select *, add_new_fields so that your downstream model has all of the fields and more for you to select from

we can talk about this in more detail during our check in tomorrow!

lag(orders.created_at, 1) [ignore nulls]
over (partition by orders.customer_id
order by orders.created_at desc) as created_at_prev,
created_at
from orders
),

final as (
select
JDW818 marked this conversation as resolved.
Show resolved Hide resolved
datediff(month, created_at, created_at_prev) as months_since_prior_order
from calc
)

select * from final