Skip to content

Commit

Permalink
Apply the stupid dup to a intermediate model and use recursive cte
Browse files Browse the repository at this point in the history
Signed-off-by: Timothy Lee <ctiml@infuseai.io>
  • Loading branch information
ctiml authored and wcchang1115 committed Sep 7, 2023
1 parent 20cab05 commit 6463187
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 25 deletions.
24 changes: 20 additions & 4 deletions models/int_customer_order_history_joined.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
with customers as (
with recursive customers as (

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

Expand Down Expand Up @@ -45,6 +45,22 @@ customer_payments as (

),

dup as (
select customer_payments.*, 1 as lvl
from customer_payments

union all

select customer_payments.*, dup.lvl + 1
from dup
join customer_payments on customer_payments.customer_id = dup.customer_id
where dup.lvl < 8000
),

dedup as (
select distinct customer_id, total_amount from dup
),

final as (

select
Expand All @@ -54,15 +70,15 @@ final as (
customer_orders.first_order,
customer_orders.most_recent_order,
customer_orders.number_of_orders,
customer_payments.total_amount as customer_lifetime_value
dedup.total_amount as customer_lifetime_value

from customers

left join customer_orders
on customers.customer_id = customer_orders.customer_id

left join customer_payments
on customers.customer_id = customer_payments.customer_id
left join dedup
on customers.customer_id = dedup.customer_id

)

Expand Down
22 changes: 1 addition & 21 deletions models/staging/stg_orders.sql
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,6 @@ renamed as (

),

dup as (
SELECT * FROM renamed
{% for i in range(0, 50) %}
UNION
SELECT * FROM renamed
{% endfor %}
),

dupdup as (
SELECT * FROM dup
{% for i in range(0, 100) %}
UNION
SELECT * FROM dup
{% endfor %}
),

dedup as (
select distinct order_id, customer_id, order_date, status from dupdup
),

-- Shift the order_date by the number of days since 2018-04-09 (the max order_date in the raw data)
shift_date as (

Expand All @@ -49,7 +29,7 @@ shift_date as (
(order_date + datediff('day', date '2018-04-09', CURRENT_DATE)::int) as order_date,
status

from dedup
from renamed
)

select * from shift_date

0 comments on commit 6463187

Please sign in to comment.