forked from dbt-labs/jaffle_shop_duckdb
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert CTEs in customers into intermediate models
Signed-off-by: Even Wei <[email protected]>
- Loading branch information
1 parent
7f3f658
commit a48228a
Showing
3 changed files
with
35 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,16 @@ | ||
with customers as ( | ||
|
||
select * from {{ ref('stg_customers') }} | ||
|
||
), | ||
|
||
orders as ( | ||
|
||
select * from {{ ref('stg_orders') }} | ||
|
||
), | ||
|
||
payments as ( | ||
|
||
select * from {{ ref('stg_payments') }} | ||
|
||
), | ||
|
||
customer_orders as ( | ||
|
||
select | ||
customer_id, | ||
|
||
min(order_date) as first_order, | ||
max(order_date) as most_recent_order, | ||
count(order_id) as number_of_orders | ||
from orders | ||
|
||
group by customer_id | ||
|
||
), | ||
|
||
customer_payments as ( | ||
|
||
select | ||
orders.customer_id, | ||
sum(amount) as total_amount | ||
|
||
from payments | ||
|
||
left join orders on | ||
payments.order_id = orders.order_id | ||
|
||
group by orders.customer_id | ||
|
||
), | ||
|
||
final as ( | ||
|
||
select | ||
customers.customer_id, | ||
customers.first_name, | ||
customers.last_name, | ||
customer_orders.first_order, | ||
customer_orders.most_recent_order, | ||
customer_orders.number_of_orders, | ||
customer_payments.total_amount as customer_lifetime_value | ||
|
||
from customers | ||
|
||
left join customer_orders | ||
on customers.customer_id = customer_orders.customer_id | ||
|
||
left join customer_payments | ||
on customers.customer_id = customer_payments.customer_id | ||
|
||
) | ||
|
||
select * from final | ||
select | ||
customers.customer_id, | ||
customers.first_name, | ||
customers.last_name, | ||
customer_orders.first_order, | ||
customer_orders.most_recent_order, | ||
customer_orders.number_of_orders, | ||
customer_payments.total_amount as customer_lifetime_value | ||
|
||
from {{ ref('stg_customers') }} customers | ||
|
||
left join {{ ref('int_customer_orders') }} customer_orders | ||
on customers.customer_id = customer_orders.customer_id | ||
|
||
left join {{ ref('int_customer_payments') }} customer_payments | ||
on customers.customer_id = customer_payments.customer_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
select | ||
customer_id, | ||
|
||
min(order_date) as first_order, | ||
max(order_date) as most_recent_order, | ||
count(order_id) as number_of_orders | ||
from {{ ref('stg_orders') }} | ||
|
||
group by customer_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
select | ||
orders.customer_id, | ||
sum(amount) as total_amount | ||
|
||
from {{ ref('stg_payments') }} payments | ||
|
||
left join {{ ref('stg_orders') }} orders on | ||
payments.order_id = orders.order_id | ||
|
||
group by orders.customer_id |