Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions SandBox_Jaggi/Unique_Customer_ID_Test_SQL_SCRIPT.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
select
count(*) as failures,
count(*) != 0 as should_warn,
count(*) != 0 as should_error
from (

select
order_id as unique_field,
count(*) as n_records

from DBT_TRAINING.dbt_jkavuru.stg_jaffle_shop__orders
where order_id is not null
group by order_id
having count(*) > 1

) dbt_internal_test

/************************************************ */

SELECT order_id, COUNT(*)
FROM DBT_TRAINING.dbt_jkavuru.stg_jaffle_shop__orders
GROUP BY order_id
HAVING COUNT(*) > 1;

19 changes: 19 additions & 0 deletions SandBox_Jaggi/create_customer_tbl.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

CREATE TABLE DBT_TRAINING.DBT_JKAVURU.customers
(
id NUMBER(9) PRIMARY KEY,
first_name VARCHAR(10) NOT NULL,
last_name VARCHAR(10) NOT NULL

);




/*

select id as customer_id, first_name, last_name

from DBT_TRAINING.DBT_JKAVURU.customers

*/
35 changes: 35 additions & 0 deletions SandBox_Jaggi/fct_orders_jk1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
with orders as (
select * from {{ ref ('stg_jaffle_shop__orders' )}}
),

payments as (
select * from {{ ref ('stg_stripe__payments') }}
),


order_payments as (
select
order_id,
payments.status as payment_status,
sum (case when status = 'success' then amount * 100 end) as amount,
sum (case when status = 'fail' then amount * 100 end) as amount_failed,
sum (case when status is null then 0 else amount * 100 end) as amount_total
from payments
group by 1,2
),

final as (

select
orders.order_id,
orders.customer_id,
orders.order_date,
coalesce (order_payments.amount, 0) as amount,
order_payments.payment_status as payment_status1
from orders
left join order_payments using (order_id)
)

select * from final order by 1 asc


15 changes: 15 additions & 0 deletions SandBox_Jaggi/jaffle_shop__sources.yml_DONOT_USE
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2

sources:
- name: jaffle_shop
database: raw
schema: jaffle_shop
freshness: # make this a little more strict
warn_after: { count: 24, period: hour }
error_after: { count: 1000, period: day } #Jaggi > increased from 12 to 9200 #
loaded_at_field: _etl_loaded_at

tables:
- name: customers
freshness: null #Jaggi, changed the freshness to null for customers model.
- name: orders
10 changes: 8 additions & 2 deletions dbt_project.yml
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks correct, we want the the project_name to be relevant to the subject area, and we want to build the staging layer as views and the marts layer as tables. Nice work.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# 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
name: 'my_new_project'
name: 'jaffle_shop'
version: '1.0.0'
config-version: 2

Expand Down Expand Up @@ -32,7 +32,13 @@ clean-targets: # directories to be removed by `dbt clean`
# as tables. These settings can be overridden in the individual model files
# using the `{{ config(...) }}` macro.
models:
my_new_project:
jaffle_shop:
# Applies to all files under models/example/
example:
materialized: view

staging:
materialized: view

marts:
materialized: table
28 changes: 16 additions & 12 deletions models/example/my_first_dbt_model.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This model could be removed from the project as it's really just a placeholder for the project.

Original file line number Diff line number Diff line change
@@ -1,27 +1,31 @@

/*
Welcome to your first dbt model!
Did you know that you can also configure models directly within SQL files?
This will override configurations stated in dbt_project.yml

Try changing "table" to "view" below
*/
{{ config(materialized="table") }}

{{ config(materialized='table') }}

with source_data as (
with
source_data as (

select 1 as id
union all
select null as id
select 1 as id, 'Jaggi' as fname, 'Krishna' as lname
union all
select 2 as id, 'Todd' as fname, 'Graham' as lname
union all
select 3 as id, 'Penny' as fname, 'Davis' as lname
union all
select null as id, 'Kelly' as fname, 'Greer' as lname

)
)

select *
from source_data
from
source_data

/*
/*
Uncomment the line below to remove records with null `id` values
*/

-- where id is not null
-- where id is not null
6 changes: 1 addition & 5 deletions models/example/my_second_dbt_model.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This model could be removed from the project as it's really just a placeholder for the project.

Original file line number Diff line number Diff line change
@@ -1,6 +1,2 @@

-- Use the `ref` function to select from other models

select *
from {{ ref('my_first_dbt_model') }}
where id = 1
select * from {{ ref("my_first_dbt_model") }} where id = 2
52 changes: 35 additions & 17 deletions models/example/schema.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@

version: 2

models:
- name: my_first_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
- name: my_first_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null

- name: FName
description: "The First Name of the Student"
tests:
- not_null

- name: LName
description: "The Last Name of the Student"
tests:
- not_null

- name: my_second_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
- name: FName
description: "The First Name of the Student"
tests:
- not_null

- name: my_second_dbt_model
description: "A starter dbt model"
columns:
- name: id
description: "The primary key for this table"
tests:
- unique
- not_null
- name: LName
description: "The Last Name of the Student"
tests:
- not_null
24 changes: 24 additions & 0 deletions models/example/source_data_view.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{{ config(materialized="view") }}

with
source_data_view as (

select 1 as id, 'Jaggi' as fname, 'Krishna' as lname
union all
select 2 as id, 'Todd' as fname, 'Graham' as lname
union all
select 3 as id, 'Penny' as fname, 'Davis' as lname
union all
select 4 as id, 'John' as fname, 'Kiwa' as lname
union all
select null as id, 'Kelly' as fname, 'Greer' as lname

)

select *
from source_data_view

/*
Uncomment the line below to remove records with null `id` values
*/
where id is not null
31 changes: 31 additions & 0 deletions models/marts/finance/fct_orders.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, nice use of ref and I like how it pulls all of the components together for a final model.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
with orders as (
select * from {{ ref ('stg_jaffle_shop__orders' )}}
),

payments as (
select * from {{ ref ('stg_stripe__payments') }}
),

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
45 changes: 45 additions & 0 deletions models/marts/marketing/dim_customers.sql
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, well formatted, proper use of the ref function. Nice work!

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
with customers as (

select * from {{ ref('stg_jaffle_shop__customers') }}

),

orders as (

select * from {{ ref('stg_jaffle_shop__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
13 changes: 13 additions & 0 deletions models/staging/jaffle_shop/jaffle_shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% docs order_status %}

One of the following values:

| status | definition |
|----------------|--------------------------------------------------|
| placed | Order placed, not yet shipped |
| shipped | Order has been shipped, not yet been delivered |
| completed | Order has been received by customers |
| return pending | Customer indicated they want to return this item |
| returned | Item has been returned |

{% enddocs %}
25 changes: 25 additions & 0 deletions models/staging/jaffle_shop/src_jaffle_shop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
version: 2

sources:
- name: jaffle_shop
description: A clone of a Postgre application database
database: raw
schema: jaffle_shop
tables:
- name: customers
description: This is raw customer data.
columns:
- name: id
description: Customer ID is the primary key
tests:
- unique
- not_null

- name: orders
description: This is raw orders data
columns:
- name: id
description: Order ID is the Primary key
tests:
- unique
- not_null
Loading