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
6 changes: 3 additions & 3 deletions models/intermediate/cross_domain/int_customer_rfm_scores.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ percentiles as (
select
customer_id,
days_since_last_order,
order_count,
total_spend,
order_count,
ntile(5) over (order by days_since_last_order desc) as recency_score,
ntile(5) over (order by order_count asc) as frequency_score,
ntile(5) over (order by total_spend asc) as monetary_score
Expand All @@ -40,12 +40,12 @@ percentiles as (
select
customer_id,
days_since_last_order,
order_count,
total_spend,
order_count,
recency_score,
frequency_score,
monetary_score,
recency_score + frequency_score + monetary_score as rfm_total_score,
monetary_score + frequency_score + recency_score as rfm_total_score,
cast(recency_score as {{ dbt.type_string() }})
|| cast(frequency_score as {{ dbt.type_string() }})
|| cast(monetary_score as {{ dbt.type_string() }}) as rfm_segment_code
Expand Down
16 changes: 4 additions & 12 deletions models/marts/customers.sql
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
-- Customer dimension with lifetime order summary
with

customers as (

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

),

orders as (

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

),

customer_orders_summary as (

select
orders.customer_id,

count(distinct orders.order_id) as count_lifetime_orders,
count(distinct orders.order_id) > 1 as is_repeat_buyer,
count(distinct orders.order_id) as count_lifetime_orders, -- total unique orders
count(distinct orders.order_id) > 1 as is_repeat_buyer, -- true if ordered more than once
min(orders.ordered_at) as first_ordered_at,
max(orders.ordered_at) as last_ordered_at,
sum(orders.subtotal) as lifetime_spend_pretax,
sum(orders.tax_paid) as lifetime_tax_paid,
sum(orders.order_total) as lifetime_spend

sum(orders.order_total) as lifetime_spend -- includes tax
from orders

group by 1

),

joined as (
Expand Down
10 changes: 5 additions & 5 deletions models/marts/order_items.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,23 @@ joined as (
select
order_items.*,

orders.ordered_at,

products.product_name,
products.product_price,
products.is_food_item,
products.is_drink_item,

orders.ordered_at,

order_supplies_summary.supply_cost

from order_items

left join orders on order_items.order_id = orders.order_id
left join products on products.product_id = order_items.product_id

left join products on order_items.product_id = products.product_id
left join orders on orders.order_id = order_items.order_id

left join order_supplies_summary
on order_items.product_id = order_supplies_summary.product_id
on order_supplies_summary.product_id = order_items.product_id

)

Expand Down
10 changes: 9 additions & 1 deletion models/marts/products.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,12 @@ products as (

)

select * from products
select
product_price,
product_name,
is_drink_item,
product_id,
product_description,
is_food_item,
product_type
from products
15 changes: 8 additions & 7 deletions models/marts/scoring/scr_store_health.sql
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,24 @@ scored as (

),

-- Composite store health score and tiering
final as (

select
location_id,
store_name,
total_revenue,
avg_operating_margin_pct,
total_revenue,
avg_labor_cost_pct,
revenue_growth_score,
profitability_score,
labor_efficiency_score,
revenue_growth_score,
inventory_health_score,
revenue_growth_score + profitability_score + labor_efficiency_score + inventory_health_score as store_health_score,
labor_efficiency_score,
profitability_score + revenue_growth_score + inventory_health_score + labor_efficiency_score as store_health_score,
case
when revenue_growth_score + profitability_score + labor_efficiency_score + inventory_health_score >= 75 then 'excellent'
when revenue_growth_score + profitability_score + labor_efficiency_score + inventory_health_score >= 50 then 'good'
when revenue_growth_score + profitability_score + labor_efficiency_score + inventory_health_score >= 25 then 'needs_improvement'
when profitability_score + revenue_growth_score + inventory_health_score + labor_efficiency_score >= 80 then 'excellent'
when profitability_score + revenue_growth_score + inventory_health_score + labor_efficiency_score >= 60 then 'good'
when profitability_score + revenue_growth_score + inventory_health_score + labor_efficiency_score >= 25 then 'needs_improvement'
else 'critical'
end as health_tier

Expand Down