forked from dbt-labs/jaffle-shop-classic
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add finance and sales models for returned order value and monthly cus…
…tomer count
- Loading branch information
1 parent
8ef7391
commit 2602696
Showing
4 changed files
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: fnl_returned_order_value | ||
meta: | ||
owner: [email protected] | ||
description: | | ||
One row per total value of returned orders per customer | ||
columns: | ||
- name: customer_id | ||
description: Primary key | ||
tests: | ||
- unique | ||
- not_null | ||
|
18 changes: 18 additions & 0 deletions
18
jaffle_shop/models/final/finance/fnl_returned_order_value.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
WITH returned_orders AS ( | ||
SELECT * | ||
FROM {{ ref('wh_orders') }} | ||
WHERE status = 'returned' | ||
) | ||
|
||
customers AS ( | ||
SELECT * | ||
FROM {{ref('stg_customers') }} | ||
) | ||
|
||
SELECT | ||
customer_id | ||
, SUM(COALESCE(total_amount, 0) AS total_value | ||
FROM returned_orders | ||
LEFT JOIN customers | ||
ON returned_orders.customer_id = customers.customer_id | ||
GROUP BY customer_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
version: 2 | ||
|
||
models: | ||
- name: fnl_monthly_customer_count | ||
meta: | ||
owner: [email protected] | ||
description: | | ||
One row per customer count for each month based on their first orders. | ||
columns: | ||
- name: first_order_month | ||
description: Primary key | ||
tests: | ||
- unique | ||
- not_null |
12 changes: 12 additions & 0 deletions
12
jaffle_shop/models/final/sales/fnl_monthly_customer_count.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
WITH customer_orders AS ( | ||
SELECT | ||
customer_id | ||
, DATE_FORMAT(first_order, 'MMMM') As order_month | ||
FROM {{ ref('wh_customers') }} | ||
) | ||
|
||
SELECT | ||
first_order AS first_order_month | ||
, COUNT(*) AS customer_count | ||
FROM customer_orders | ||
GROUP BY first_order |