diff --git a/jaffle_shop/models/customer_total_spend.sql b/jaffle_shop/models/customer_total_spend.sql new file mode 100644 index 0000000..d89a9a2 --- /dev/null +++ b/jaffle_shop/models/customer_total_spend.sql @@ -0,0 +1,30 @@ +{{ + config( + materialized='table' + ) +}} + +with orders as ( + select * from {{ ref('stg_orders') }} +), + +customer_spend as ( + select + customer_id, + sum(order_amount) as total_spend + from orders + group by customer_id +), + +customers as ( + select * from {{ ref('stg_customers') }} +) + +select + customers.customer_id, + customers.first_name, + customers.last_name, + coalesce(customer_spend.total_spend, 0) as total_spend +from customers +left join customer_spend using (customer_id) +order by total_spend desc \ No newline at end of file