Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit 63855d9

Browse files
committed
Amend models to be warehouse
1 parent bb49ab3 commit 63855d9

6 files changed

Lines changed: 96 additions & 131 deletions

File tree

jaffle_shop/models/customers.sql

Lines changed: 0 additions & 69 deletions
This file was deleted.

jaffle_shop/models/orders.sql

Lines changed: 0 additions & 56 deletions
This file was deleted.
File renamed without changes.
Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
version: 2
22

33
models:
4-
- name: customers
5-
description: This table has basic information about a customer, as well as some derived facts based on a customer's orders
4+
- name: wh_customers
5+
access: public
6+
config:
7+
group: industry_operations
8+
meta:
9+
owner: luke.ralphs-davies@octoenergy.com
10+
description: |
11+
This table has basic information about a customer, as well as some derived facts based on a customer's orders
612
713
columns:
814
- name: customer_id
@@ -12,10 +18,10 @@ models:
1218
- not_null
1319

1420
- name: first_name
15-
description: Customer's first name. PII.
21+
description: Customer's first name (hashed).
1622

1723
- name: last_name
18-
description: Customer's last name. PII.
24+
description: Customer's last name (hashed).
1925

2026
- name: first_order
2127
description: Date (UTC) of a customer's first order
@@ -29,8 +35,14 @@ models:
2935
- name: total_order_amount
3036
description: Total value (AUD) of a customer's orders
3137

32-
- name: orders
33-
description: This table has basic information about orders, as well as some derived facts based on payments
38+
- name: wh_orders
39+
access: public
40+
config:
41+
group: industry_operations
42+
meta:
43+
owner: luke.ralphs-davies@octoenergy.com
44+
description: |
45+
This table has basic information about orders, as well as some derived facts based on payments.
3446
3547
columns:
3648
- name: order_id
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
WITH customer_orders as (
2+
SELECT
3+
customer_id
4+
, MIN(order_date) AS first_order
5+
, MAX(order_date) as most_recent_order
6+
, COUNT(order_id) as number_of_orders
7+
FROM {{ ref('stg_orders') }}
8+
9+
GROUP BY customer_id
10+
11+
),
12+
13+
customer_payments as (
14+
SELECT
15+
orders.customer_id
16+
, SUM(payments.amount) AS total_amount
17+
18+
FROM {{ ref('stg_payments') }} AS payments
19+
20+
LEFT JOIN orders ON
21+
payments.order_id = orders.order_id
22+
23+
GROUP BY orders.customer_id
24+
25+
),
26+
27+
SELECT
28+
customers.customer_id
29+
, customers.first_name
30+
, customers.last_name
31+
, customer_orders.first_order
32+
, customer_orders.most_recent_order
33+
, customer_orders.number_of_orders
34+
, customer_payments.total_amount AS customer_lifetime_value
35+
36+
FROM {{ ref('stg_customers') }} AS customers
37+
38+
LEFT JOIN customer_orders
39+
ON customers.customer_id = customer_orders.customer_id
40+
41+
LEFT JOIN customer_payments
42+
ON customers.customer_id = customer_payments.customer_id
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
{% set payment_methods = ['credit_card', 'coupon', 'bank_transfer', 'gift_card'] %}
2+
3+
WITH order_payments AS (
4+
SELECT
5+
order_id
6+
7+
{% for payment_method in payment_methods -%}
8+
, SUM(CASE WHEN payment_method = '{{ payment_method }}' THEN amount ELSE 0 END) AS {{ payment_method }}_amount
9+
{% endfor -%}
10+
11+
, SUM(amount) AS total_amount
12+
13+
FROM {{ ref('stg_payments') }}
14+
15+
GROUP BY order_id
16+
17+
),
18+
19+
SELECT
20+
orders.order_id
21+
, orders.customer_id
22+
, orders.order_date
23+
, orders.status
24+
25+
{% for payment_method in payment_methods -%}
26+
27+
, order_payments.{{ payment_method }}_amount
28+
{% endfor -%}
29+
30+
, order_payments.total_amount AS amount
31+
32+
FROM {{ ref('stg_orders') }} AS orders
33+
34+
35+
LEFT JOIN order_payments
36+
ON orders.order_id = order_payments.order_id

0 commit comments

Comments
 (0)