Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding ecommerce example #6

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
Empty file added 003_ecommerce/audits/.gitkeep
Empty file.
9 changes: 9 additions & 0 deletions 003_ecommerce/audits/assert_positive_order_ids.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
AUDIT (
name assert_positive_order_ids
);

SELECT
*
FROM @this_model
WHERE
item_id < 0
11 changes: 11 additions & 0 deletions 003_ecommerce/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
gateways:
local:
connection:
type: duckdb
database: db.db

default_gateway: local

model_defaults:
dialect: duckdb
start: 2024-12-15
Empty file added 003_ecommerce/macros/.gitkeep
Empty file.
Empty file.
Empty file added 003_ecommerce/models/.gitkeep
Empty file.
42 changes: 42 additions & 0 deletions 003_ecommerce/models/bronze/raw_customer_addresses.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
MODEL (
name bronze.raw_customer_addresses,
kind INCREMENTAL_BY_TIME_RANGE (
time_column updated_at
),
grain [address_id],
tags ['bronze'],
columns (
address_id INT,
customer_id INT,
address_type TEXT,
street_address TEXT,
city TEXT,
state TEXT,
postal_code TEXT,
country TEXT,
is_default BOOLEAN,
created_at TIMESTAMP,
updated_at TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_customer_addresses]
);

SELECT
id AS address_id,
customer_id,
address_type,
street_address,
city,
state,
postal_code,
country,
is_default,
created_at,
updated_at,
_loaded_at,
_file_name
FROM source_ecommerce.raw_customer_addresses
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
34 changes: 34 additions & 0 deletions 003_ecommerce/models/bronze/raw_customers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
MODEL (
name bronze.raw_customers,
kind INCREMENTAL_BY_TIME_RANGE (
time_column updated_at
),
grain [customer_id],
tags ['bronze'],
columns (
customer_id INT,
email TEXT,
first_name TEXT,
last_name TEXT,
phone_number TEXT,
created_at TIMESTAMP,
updated_at TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_customers]
);

SELECT
id AS customer_id,
email,
first_name,
last_name,
phone_number,
created_at,
updated_at,
_loaded_at,
_file_name
FROM source_ecommerce.raw_customers
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
32 changes: 32 additions & 0 deletions 003_ecommerce/models/bronze/raw_order_items.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
MODEL (
name bronze.raw_order_items,
kind INCREMENTAL_BY_TIME_RANGE (
time_column order_timestamp
),
grain [order_item_id],
tags ['bronze'],
columns (
order_item_id INT,
order_id INT,
product_id INT,
quantity INT,
unit_price DECIMAL(10, 2),
order_timestamp TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_order_items]
);

SELECT
id AS order_item_id,
order_id,
product_id,
quantity,
unit_price,
created_at AS order_timestamp,
_loaded_at,
_file_name
FROM source_ecommerce.raw_order_items
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
30 changes: 30 additions & 0 deletions 003_ecommerce/models/bronze/raw_orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
MODEL (
name bronze.raw_orders,
kind INCREMENTAL_BY_TIME_RANGE (
time_column order_timestamp
),
grain [order_id],
tags ['bronze'],
columns (
order_id INT,
user_id INT,
total_amount DECIMAL(10, 2),
status TEXT,
order_timestamp TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_orders]
);

SELECT
id AS order_id,
user_id,
total_amount,
status,
created_at AS order_timestamp,
_loaded_at,
_file_name
FROM source_ecommerce.raw_orders
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
32 changes: 32 additions & 0 deletions 003_ecommerce/models/bronze/raw_product_categories.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
MODEL (
name bronze.raw_product_categories,
kind INCREMENTAL_BY_TIME_RANGE (
time_column updated_at
),
grain [category_id],
tags ['bronze'],
columns (
category_id INT,
category_name TEXT,
parent_category_id INT,
description TEXT,
created_at TIMESTAMP,
updated_at TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_product_categories]
);

SELECT
id AS category_id,
name AS category_name,
parent_category_id,
description,
created_at,
updated_at,
_loaded_at,
_file_name
FROM source_ecommerce.raw_product_categories
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
40 changes: 40 additions & 0 deletions 003_ecommerce/models/bronze/raw_products.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
MODEL (
name bronze.raw_products,
kind INCREMENTAL_BY_TIME_RANGE (
time_column updated_at
),
grain [product_id],
tags ['bronze'],
columns (
product_id INT,
sku TEXT,
product_name TEXT,
description TEXT,
category_id INT,
supplier_id INT,
unit_price DECIMAL(10, 2),
stock_quantity INT,
created_at TIMESTAMP,
updated_at TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_products]
);

SELECT
id AS product_id,
sku,
name AS product_name,
description,
category_id,
supplier_id,
unit_price,
stock_quantity,
created_at,
updated_at,
_loaded_at,
_file_name
FROM source_ecommerce.raw_products
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
36 changes: 36 additions & 0 deletions 003_ecommerce/models/bronze/raw_shipments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
MODEL (
name bronze.raw_shipments,
kind INCREMENTAL_BY_TIME_RANGE (
time_column updated_at
),
grain [shipment_id],
tags ['bronze'],
columns (
shipment_id INT,
order_id INT,
tracking_number TEXT,
status TEXT,
estimated_delivery_date DATE,
actual_delivery_date DATE,
created_at TIMESTAMP,
updated_at TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_shipments]
);

SELECT
id AS shipment_id,
order_id,
tracking_number,
status,
estimated_delivery_date,
actual_delivery_date,
created_at,
updated_at,
_loaded_at,
_file_name
FROM source_ecommerce.raw_shipments
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
44 changes: 44 additions & 0 deletions 003_ecommerce/models/bronze/raw_suppliers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
MODEL (
name bronze.raw_suppliers,
kind INCREMENTAL_BY_TIME_RANGE (
time_column updated_at
),
grain [supplier_id],
tags ['bronze'],
columns (
supplier_id INT,
company_name TEXT,
contact_name TEXT,
contact_email TEXT,
contact_phone TEXT,
address TEXT,
city TEXT,
state TEXT,
postal_code TEXT,
country TEXT,
created_at TIMESTAMP,
updated_at TIMESTAMP,
_loaded_at TIMESTAMP,
_file_name TEXT
),
references [source_ecommerce.raw_suppliers]
);

SELECT
id AS supplier_id,
company_name,
contact_name,
contact_email,
contact_phone,
address,
city,
state,
postal_code,
country,
created_at,
updated_at,
_loaded_at,
_file_name
FROM source_ecommerce.raw_suppliers
WHERE
_loaded_at >= @start_date AND _loaded_at < @end_date
33 changes: 33 additions & 0 deletions 003_ecommerce/models/gold/customer_metrics.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
MODEL (
name gold.customer_metrics,
kind FULL,
grain [customer_id],
tags ['gold'],
references [silver.customers, silver.orders, bronze.raw_customer_addresses]
);

SELECT
c.customer_id,
c.full_name,
c.email,
COUNT(DISTINCT o.order_id) AS total_orders,
SUM(o.total_amount) AS total_spend,
AVG(o.total_amount) AS average_order_value,
MIN(o.order_timestamp) AS first_order_date,
MAX(o.order_timestamp) AS last_order_date,
DATE_DIFF('DAY', MIN(o.order_timestamp), MAX(o.order_timestamp)) AS customer_lifetime_days,
ca.city,
ca.state,
ca.country
FROM silver.customers AS c
left join bronze.raw_customer_addresses as ca
on c.customer_id = ca.customer_id
LEFT JOIN silver.orders AS o
ON c.customer_id = o.user_id
GROUP BY
1,
2,
3,
10,
11,
12
Loading