diff --git a/jaffle_shop/dbt_project.yml b/jaffle_shop/dbt_project.yml index 8da2df2..9ae8088 100644 --- a/jaffle_shop/dbt_project.yml +++ b/jaffle_shop/dbt_project.yml @@ -1,4 +1,3 @@ - # Name your project! Project names should contain only lowercase characters # and underscores. A good package name should reflect your organization's # name or the intended use of these models @@ -22,14 +21,13 @@ clean-targets: # directories to be removed by `dbt clean` - "target" - "dbt_packages" - # Configuring models # Full documentation: https://docs.getdbt.com/docs/configuring-models -# In this example config, we tell dbt to build all models in the example/ -# directory as views. These settings can be overridden in the individual model +# In this example config, we tell dbt to build all models in the jaffle_shop/ +# directory as tables. These settings can be overridden in the individual model # files using the `{{ config(...) }}` macro. models: jaffle_shop: - # Config indicated by + and applies to all files under models/example/ - +materialized: table + # Config indicated by + and applies to all files under models/jaffle_shop/ + +materialized: table \ No newline at end of file diff --git a/jaffle_shop/models/customers.sql b/jaffle_shop/models/customers.sql index 49c3a70..38769a7 100644 --- a/jaffle_shop/models/customers.sql +++ b/jaffle_shop/models/customers.sql @@ -5,46 +5,32 @@ }} with customers as ( - select * from {{ ref('stg_customers') }} - ), orders as ( - select * from {{ ref('stg_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 - 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 - from customers - left join customer_orders using (customer_id) - ) select * from final \ No newline at end of file diff --git a/jaffle_shop/models/schema.yml b/jaffle_shop/models/schema.yml index 9e03d8a..bb6317e 100644 --- a/jaffle_shop/models/schema.yml +++ b/jaffle_shop/models/schema.yml @@ -1,4 +1,5 @@ version: 2 + models: - name: customers description: One record per customer @@ -17,20 +18,21 @@ models: dimension: type: date - name: first_name - description: "" + description: Customer's first name meta: dimension: type: string - name: most_recent_order_date - description: "" + description: Date of the customer's most recent order meta: dimension: type: date - name: number_of_orders - description: "" + description: Total number of orders placed by the customer meta: dimension: type: number + - name: stg_customers description: This model cleans up customer data columns: @@ -43,15 +45,16 @@ models: dimension: type: number - name: first_name - description: "" + description: Customer's first name meta: dimension: type: string - name: last_name - description: "" + description: Customer's last name meta: dimension: type: string + - name: stg_orders description: This model cleans up order data columns: @@ -64,6 +67,7 @@ models: dimension: type: number - name: status + description: Current status of the order tests: - accepted_values: values: @@ -72,7 +76,6 @@ models: - completed - return_pending - returned - description: "" meta: dimension: type: string @@ -80,17 +83,17 @@ models: total_order_count: type: count - name: customer_id + description: Foreign key to stg_customers tests: - not_null - relationships: to: ref('stg_customers') field: customer_id - description: "" meta: dimension: type: number - name: order_date - description: "" + description: Date when the order was placed meta: dimension: - type: date + type: date \ No newline at end of file diff --git a/jaffle_shop/models/staging/stg_customers.sql b/jaffle_shop/models/staging/stg_customers.sql index e09822b..bc08df5 100644 --- a/jaffle_shop/models/staging/stg_customers.sql +++ b/jaffle_shop/models/staging/stg_customers.sql @@ -4,9 +4,16 @@ ) }} -select - id as customer_id, - first_name, - last_name +with source as ( + select * from {{ source('jaffle_shop', 'customers') }} +), -from `dbt-tutorial`.jaffle_shop.customers \ No newline at end of file +renamed as ( + select + id as customer_id, + first_name, + last_name + from source +) + +select * from renamed \ No newline at end of file diff --git a/jaffle_shop/models/staging/stg_orders.sql b/jaffle_shop/models/staging/stg_orders.sql index b9906a6..a770e13 100644 --- a/jaffle_shop/models/staging/stg_orders.sql +++ b/jaffle_shop/models/staging/stg_orders.sql @@ -4,10 +4,17 @@ ) }} -select - id as order_id, - user_id as customer_id, - order_date, - status +with source as ( + select * from {{ source('jaffle_shop', 'orders') }} +), -from `dbt-tutorial`.jaffle_shop.orders \ No newline at end of file +renamed as ( + select + id as order_id, + user_id as customer_id, + order_date, + status + from source +) + +select * from renamed \ No newline at end of file