Skip to content
Draft
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
12 changes: 6 additions & 6 deletions models/business_vault/customer_pit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
{%- set yaml_metadata -%}
source_model: hub_customer
src_pk: CUSTOMER_PK
as_of_dates_table: AS_OF_DATE
as_of_dates_table: as_of_date
satellites:
SAT_CUSTOMER:
sat_customer:
pk:
PK: CUSTOMER_PK
ldts:
LDTS: LOAD_DATE
SAT_CUSTOMER_CRM:
sat_customer_crm:
pk:
PK: CUSTOMER_PK
ldts:
LDTS: LOAD_DATE
stage_tables:
STG_CUSTOMERS: LOAD_DATE
STG_CUSTOMERS_CRM: LOAD_DATE
stage_tables_ldts:
stg_customers: LOAD_DATE
stg_customers_crm: LOAD_DATE
src_ldts: LOAD_DATE
{%- endset -%}

Expand Down
27 changes: 17 additions & 10 deletions models/business_vault/our_customer_pit.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
with all_history (
select hc.customer_pk,
sc.last_name,
sc.first_name,
scc.age,
sc.effective_from as sc_effective_from,
coalesce(lead(sc.effective_from) OVER (PARTITION BY hc.customer_pk ORDER BY sc.effective_from), '9999-12-31') as sc_effective_to,
scc.effective_from as scc_effective_from,
coalesce(lead(scc.effective_from) OVER (PARTITION BY hc.customer_pk ORDER BY scc.effective_from), '9999-12-31') as scc_effective_to
with all_history as (
select
hc.customer_pk,
sc.last_name,
sc.first_name,
scc.age,
sc.effective_from as sc_effective_from,
coalesce(lead(sc.effective_from) OVER (PARTITION BY hc.customer_pk ORDER BY sc.effective_from), '9999-12-31') as sc_effective_to,
scc.effective_from as scc_effective_from,
coalesce(lead(scc.effective_from) OVER (PARTITION BY hc.customer_pk ORDER BY scc.effective_from), '9999-12-31') as scc_effective_to
from {{ref('hub_customer')}} hc
LEFT JOIN {{ref('sat_customer')}} sc ON sc.customer_pk = hc.customer_pk
LEFT JOIN {{ref('sat_customer_crm')}} scc ON scc.customer_pk = hc.customer_pk
)

SELECT customer_pk, last_name, first_name, age, sc_effective_from, scc_effective_from
SELECT
customer_pk,
last_name,
first_name,
age,
sc_effective_from,
scc_effective_from
from all_history
where 1=1
25 changes: 25 additions & 0 deletions models/mart/top_customers_completed_orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{{ config(materialized='table') }}

WITH completed_orders AS (
SELECT
user_id,
COUNT(*) AS completed_order_count
FROM {{ ref('stg_orders') }} so
WHERE LOWER(so.status) = 'завершен'
GROUP BY user_id
),
customer_info AS (
SELECT
sc.id,
sc.first_name,
sc.last_name,
sc.email
FROM {{ ref('stg_customers') }} sc
)

SELECT
ci.*,
COALESCE(co.completed_order_count, 0) AS completed_orders_count
FROM customer_info ci
LEFT JOIN completed_orders co ON ci.id = co.user_id
ORDER BY COALESCE(co.completed_order_count, 0) DESC
10 changes: 10 additions & 0 deletions models/mart/weekly_orders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{{ config(materialized='table') }}

SELECT
DATE_TRUNC('week', so.order_date) AS week_start,
COUNT(*) AS total_orders,
COUNT(DISTINCT so.user_id) AS unique_customers
FROM {{ ref('stg_orders') }} so
WHERE so.order_date IS NOT NULL
GROUP BY DATE_TRUNC('week', so.order_date)
ORDER BY week_start DESC
5 changes: 4 additions & 1 deletion seeds/source_customers.csv
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,7 @@ id,first_name,last_name,email
101,Donald,Trump,d.trump@usa.gov
102,Peter,Erickson,erickson1993@fake.com
103,Elena,Klum,e.klum@fake.com
104,Donald,Trump,d.trump3@usa.gov
104,Donald,Trump,d.trump3@usa.gov
15010,Test,Testov1,t1.test@test.gov
15011,Test,Testov2,t2.test@test.gov
15012,Test,Testov3,t3.test@test.gov
7 changes: 0 additions & 7 deletions seeds/source_orders.csv
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
id,user_id,order_date,status
1,1,2018-01-01,returned
2,3,2018-01-02,completed
3,94,2018-01-04,completed
4,50,2018-01-05,completed
5,64,2018-01-05,completed
6,54,2018-01-07,completed
7,88,2018-01-09,completed
8,2,2018-01-11,returned
9,53,2018-01-12,completed
10,7,2018-01-14,completed
Expand Down