Skip to content

Commit

Permalink
Add finance and sales models for returned order value and monthly cus…
Browse files Browse the repository at this point in the history
…tomer count
  • Loading branch information
joshuadias2212 committed Apr 10, 2024
1 parent 8ef7391 commit 2602696
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions jaffle_shop/models/final/finance/_models.yml
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 jaffle_shop/models/final/finance/fnl_returned_order_value.sql
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
14 changes: 14 additions & 0 deletions jaffle_shop/models/final/sales/_models.yml
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 jaffle_shop/models/final/sales/fnl_monthly_customer_count.sql
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

0 comments on commit 2602696

Please sign in to comment.