From 961a2430508fb2056d58fab16ac03fe771d2f86e Mon Sep 17 00:00:00 2001 From: Keith Fajardo Date: Thu, 5 Dec 2024 01:49:01 +0000 Subject: [PATCH] Commit changes --- .../stg_jaffle_shop__customers.sql | 53 ------------------- .../jaffle_shop/stg_jaffle_shop__orders.sql | 48 ----------------- .../_semantic_layer_sources.yml | 15 ++++++ .../marts/dim_customer.sql | 29 ++++++++++ .../semantic_layer_demo/marts/fact_orders.sql | 43 +++++++++++++++ .../jaffle_shop/_jaffle_shop__models.yml | 0 .../jaffle_shop/_jaffle_shop__sources.yml | 0 .../stg_jaffle_shop__customers.sql | 11 ++++ .../jaffle_shop/stg_jaffle_shop__orders.sql | 12 +++++ .../staging/stripe/stg_stripe__payment.sql | 17 ++++++ .../staging/stripe/stg_stripe__payments.sql | 2 +- 11 files changed, 128 insertions(+), 102 deletions(-) delete mode 100644 models/_samples/staging/jaffle_shop/stg_jaffle_shop__customers.sql delete mode 100644 models/_samples/staging/jaffle_shop/stg_jaffle_shop__orders.sql create mode 100644 models/semantic_layer_demo/_semantic_layer_sources.yml create mode 100644 models/semantic_layer_demo/marts/dim_customer.sql create mode 100644 models/semantic_layer_demo/marts/fact_orders.sql rename models/{_samples => semantic_layer_demo}/staging/jaffle_shop/_jaffle_shop__models.yml (100%) rename models/{_samples => semantic_layer_demo}/staging/jaffle_shop/_jaffle_shop__sources.yml (100%) create mode 100644 models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__customers.sql create mode 100644 models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__orders.sql create mode 100644 models/semantic_layer_demo/staging/stripe/stg_stripe__payment.sql diff --git a/models/_samples/staging/jaffle_shop/stg_jaffle_shop__customers.sql b/models/_samples/staging/jaffle_shop/stg_jaffle_shop__customers.sql deleted file mode 100644 index 0e10767e..00000000 --- a/models/_samples/staging/jaffle_shop/stg_jaffle_shop__customers.sql +++ /dev/null @@ -1,53 +0,0 @@ -{#- -Explaining Configurations - - grants: shows how to grant a specific privilege (in this case select) on the object to a role in Snowflake (in this case transformer) - - post_hook: shows you can run a statement after your model is built - - alias: sets the name of the object that will be created in the warehouse and overrides the file name. in this case, the model - will b created as staging_jaffle_shop_customers - - materialized: overwrites the materialization strategy from the dbt_project.yml file to be table instead of view - - persist_docs: persists the documentation into the database - - schema: adjusting the schema that the model gets built in to be a concatenation of the target schema and "jaffle_shop" - - database: example of how you can overwrite the target database by setting the database in the config. in this example, - we're overriding to the analytics database (even though that is the same as the target database, just an example) - --#} - -{{ - config( - grants = {'select': ['transformer']}, - post_hook = 'select * from {{ this }} limit 1', - alias = 'staging_jaffle_shop_customers', - materialized = 'table', - persist_docs = {"relation": true, "columns": true}, - tags = ['finance'], - schema = 'jaffle_shop', - database = 'analytics' - ) -}} - -{{ - config( - materialized='table' - ) -}} - - - -with source as ( - - select * from {{ source('jaffle_shop', 'customers') }} - -), - -renamed as ( - - select - id as customer_id, - first_name, - last_name - - from source - -) - -select * from renamed diff --git a/models/_samples/staging/jaffle_shop/stg_jaffle_shop__orders.sql b/models/_samples/staging/jaffle_shop/stg_jaffle_shop__orders.sql deleted file mode 100644 index afa5cf86..00000000 --- a/models/_samples/staging/jaffle_shop/stg_jaffle_shop__orders.sql +++ /dev/null @@ -1,48 +0,0 @@ -{#- -Explaining Configurations - - grants: shows how to grant a specific privilege (in this case select) on the object to a role in Snowflake (in this case transformer) - - post_hook: shows you can run a statement after your model is built - - alias: sets the name of the object that will be created in the warehouse and overrides the file name. in this case, the model - will b created as staging_jaffle_shop_orders - - materialized: overwrites the materialization strategy from the dbt_project.yml file to be table instead of view - - persist_docs: persists the documentation into the database - - schema: adjusting the schema that the model gets built in to be a concatenation of the target schema and "jaffle_shop" - - database: example of how you can overwrite the target database by setting the database in the config. in this example, - we're overriding to the analytics database (even though that is the same as the target database, just an example) - --#} - -{{ - config( - grants = {'select': ['transformer']}, - post_hook = 'select * from {{ this }} limit 1', - alias = 'staging_jaffle_shop_orders', - materialized = 'table', - persist_docs = {"relation": true, "columns": true}, - tags = ['finance', 'orders'], - schema = 'jaffle_shop', - database = 'analytics' - ) -}} - - -with source as ( - - select * from {{ source('jaffle_shop', 'orders') }} - -), - -renamed as ( - - select - id as order_id, - user_id as customer_id, - order_date, - status, - _etl_loaded_at - - from source - -) - -select * from renamed diff --git a/models/semantic_layer_demo/_semantic_layer_sources.yml b/models/semantic_layer_demo/_semantic_layer_sources.yml new file mode 100644 index 00000000..aff5be45 --- /dev/null +++ b/models/semantic_layer_demo/_semantic_layer_sources.yml @@ -0,0 +1,15 @@ +version: 2 + +sources: + - name: orders + database: raw + schema: jaffle_shop + tables: + - name: orders + - name: customers + + - name: payments + database: raw + schema: stripe + tables: + - name: payment \ No newline at end of file diff --git a/models/semantic_layer_demo/marts/dim_customer.sql b/models/semantic_layer_demo/marts/dim_customer.sql new file mode 100644 index 00000000..e7bc0723 --- /dev/null +++ b/models/semantic_layer_demo/marts/dim_customer.sql @@ -0,0 +1,29 @@ +with customers as ( + select * from {{ ref('stg_jaffle_shop__customers')}} +), +orders as ( + select * from {{ ref('fact_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, + sum(amount) as lifetime_value + 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, + customer_orders.lifetime_value + from customers + left join customer_orders using (customer_id) +) +select * from final diff --git a/models/semantic_layer_demo/marts/fact_orders.sql b/models/semantic_layer_demo/marts/fact_orders.sql new file mode 100644 index 00000000..53ae30e5 --- /dev/null +++ b/models/semantic_layer_demo/marts/fact_orders.sql @@ -0,0 +1,43 @@ +{{ + config( + tags=["semantic_layer_demo"] + ) +}} + +with orders as ( + select * from {{ ref('stg_jaffle_shop__orders' )}} +), + + +payments as ( + select * from {{ ref('stg_stripe__payment') }} +), + + +order_payments as ( + select + order_id, + sum(case when status = 'success' then amount end) as amount + + + from payments + group by 1 +), + + +final as ( + + + select + orders.order_id, + orders.customer_id, + orders.order_date, + coalesce(order_payments.amount, 0) as amount + + + from orders + left join order_payments using (order_id) +) + + +select * from final diff --git a/models/_samples/staging/jaffle_shop/_jaffle_shop__models.yml b/models/semantic_layer_demo/staging/jaffle_shop/_jaffle_shop__models.yml similarity index 100% rename from models/_samples/staging/jaffle_shop/_jaffle_shop__models.yml rename to models/semantic_layer_demo/staging/jaffle_shop/_jaffle_shop__models.yml diff --git a/models/_samples/staging/jaffle_shop/_jaffle_shop__sources.yml b/models/semantic_layer_demo/staging/jaffle_shop/_jaffle_shop__sources.yml similarity index 100% rename from models/_samples/staging/jaffle_shop/_jaffle_shop__sources.yml rename to models/semantic_layer_demo/staging/jaffle_shop/_jaffle_shop__sources.yml diff --git a/models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__customers.sql b/models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__customers.sql new file mode 100644 index 00000000..6284f71a --- /dev/null +++ b/models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__customers.sql @@ -0,0 +1,11 @@ +{{ + config( + tags=['semantic_layer_demo'] + ) +}} + + select + id as customer_id, + first_name, + last_name +from {{ source('jaffle_shop', 'customers') }} \ No newline at end of file diff --git a/models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__orders.sql b/models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__orders.sql new file mode 100644 index 00000000..6be9048d --- /dev/null +++ b/models/semantic_layer_demo/staging/jaffle_shop/stg_jaffle_shop__orders.sql @@ -0,0 +1,12 @@ +{{ + config( + tags=['semantic_layer_demo'] + ) +}} + + select + id as order_id, + user_id as customer_id, + order_date, + status + from {{ source('jaffle_shop', 'orders') }} \ No newline at end of file diff --git a/models/semantic_layer_demo/staging/stripe/stg_stripe__payment.sql b/models/semantic_layer_demo/staging/stripe/stg_stripe__payment.sql new file mode 100644 index 00000000..e0bc13ff --- /dev/null +++ b/models/semantic_layer_demo/staging/stripe/stg_stripe__payment.sql @@ -0,0 +1,17 @@ +{{ + config( + tags=['semantic_layer_demo'] + ) +}} + +select + id as payment_id, + orderid as order_id, + paymentmethod as payment_method, + status, + -- amount is stored in cents, convert it to dollars + amount / 100 as amount, + created as created_at + + +from {{ source('stripe', 'payment') }} \ No newline at end of file diff --git a/models/staging/stripe/stg_stripe__payments.sql b/models/staging/stripe/stg_stripe__payments.sql index 61ede9e5..61fd513d 100644 --- a/models/staging/stripe/stg_stripe__payments.sql +++ b/models/staging/stripe/stg_stripe__payments.sql @@ -13,6 +13,6 @@ select -- datetimes created as created_at -from {{ ref('snapshot_stg_payments') }} +from {{ source('stripe', 'payment') }} -- pull only the most recent update for each unique record where dbt_valid_to is null