diff --git a/jaffle_shop/models/final/finance/_models.yml b/jaffle_shop/models/final/finance/_models.yml new file mode 100644 index 000000000..ced7510c5 --- /dev/null +++ b/jaffle_shop/models/final/finance/_models.yml @@ -0,0 +1,13 @@ +version: 2 + +models: + - name: fnl_finance_customerreturnstotal + description: > + This table gives one row per customer, with the total of their completed returns, + pending_returns and the sum of the 2 + columns: + - name: customer_id + description: primary key + tests: + - unique + - not_null diff --git a/jaffle_shop/models/final/finance/fnl_finance_customerreturnstotal.sql b/jaffle_shop/models/final/finance/fnl_finance_customerreturnstotal.sql new file mode 100644 index 000000000..a3d2c59dc --- /dev/null +++ b/jaffle_shop/models/final/finance/fnl_finance_customerreturnstotal.sql @@ -0,0 +1,18 @@ +{% set return_states = ['returned', 'return_pending'] %} + +select + custs.customer_id + + {% for return_state in return_states -%} + , SUM(payments.amount_dollars) FILTER (WHERE orders.status =' {{ return_state }}') AS {{ return_state }}_amount_dollars + {% endfor -%} + + , SUM(payments.amount_dollars) AS sum_return_amount_dollars + +from {{ ref('wh_orders') }} AS orders + left join {{ ref('wh_customers') }} AS custs + on custs.customer_id = orders.customer_id + left join {{ ref('stg_payments') }} AS payments + on payments.order_id = orders.order_id +where orders.status IN ('returned', 'return_pending') +GROUP BY customer_id \ No newline at end of file diff --git a/jaffle_shop/models/final/sales/_models.yml b/jaffle_shop/models/final/sales/_models.yml new file mode 100644 index 000000000..8186417cf --- /dev/null +++ b/jaffle_shop/models/final/sales/_models.yml @@ -0,0 +1,13 @@ +version: 2 + +models: + - name: fnl_sales_newcustomerhistory + description: > + This table gives one row per truncated month with the count + of customers that have created their first order within this month + columns: + - name: first_order_month + description: primary key + tests: + - unique + - not_null \ No newline at end of file diff --git a/jaffle_shop/models/final/sales/fnl_sales_newcustomerhistory.sql b/jaffle_shop/models/final/sales/fnl_sales_newcustomerhistory.sql new file mode 100644 index 000000000..8258f25f4 --- /dev/null +++ b/jaffle_shop/models/final/sales/fnl_sales_newcustomerhistory.sql @@ -0,0 +1,4 @@ +select + date_trunc('month', first_order) AS first_order_month + , count(*) AS number_customers +from {{ ref('wh_customers') }} \ No newline at end of file diff --git a/jaffle_shop/models/intermediate/_models.yml b/jaffle_shop/models/intermediate/_models.yml index e69de29bb..ea428f796 100644 --- a/jaffle_shop/models/intermediate/_models.yml +++ b/jaffle_shop/models/intermediate/_models.yml @@ -0,0 +1 @@ +version: 2 \ No newline at end of file diff --git a/jaffle_shop/models/staging/src_seed/stg_payments.sql b/jaffle_shop/models/staging/src_seed/stg_payments.sql index 700cf7f4f..502a3ecdc 100644 --- a/jaffle_shop/models/staging/src_seed/stg_payments.sql +++ b/jaffle_shop/models/staging/src_seed/stg_payments.sql @@ -16,7 +16,7 @@ renamed as ( payment_method, -- `amount` is currently stored in cents, so we convert it to dollars - amount / 100 as amount + amount / 100 as amount_dollars from source diff --git a/jaffle_shop/models/warehouse/customers.sql b/jaffle_shop/models/warehouse/wh_customers.sql similarity index 99% rename from jaffle_shop/models/warehouse/customers.sql rename to jaffle_shop/models/warehouse/wh_customers.sql index 016a004fe..3f96d6e00 100644 --- a/jaffle_shop/models/warehouse/customers.sql +++ b/jaffle_shop/models/warehouse/wh_customers.sql @@ -51,6 +51,7 @@ final as ( customers.customer_id, customers.first_name, customers.last_name, + customer_orders.first_order, customer_orders.most_recent_order, customer_orders.number_of_orders, diff --git a/jaffle_shop/models/warehouse/orders.sql b/jaffle_shop/models/warehouse/wh_orders.sql similarity index 100% rename from jaffle_shop/models/warehouse/orders.sql rename to jaffle_shop/models/warehouse/wh_orders.sql