You are on page 1of 33

--alter session set week_start = 1

--select date_trunc('week', current_date)

with journey_entrep

as

(select * from journey.entrep where country='PE'

qualify row_number() over (partition by dynamo_leader_id order by create_date_tz)=1

vuupt

as

select date_trunc('month',first_etd_date::date) as _month,

store_url,

count(distinct iff(first_day=1 and failed_reason_treated is not null,store_url,null)) as


tiendas_con_pedido_no_entregado,

count(distinct iff(first_day=1 and (failed_reason_treated is not null or


(units_reembolso+units_cupon+units_reposicion+units_faltante_anunciado)>0),store_url,null)) as
tiendas_con_problemas_logisticos

from favodata.snp_sandbox.logistics_transportation_delivery_details_v2

where create_date_time_tz::date>='2022-01-01'

and first_day=1

group by 1,2

order by 1 desc

base

as
(select

je.distrito,

g.macrozone,

je.leader_name,

je.phone_number,

b.store_url,

b.dynamo_leader_id,

concat(year(b.activation_date),'-',month(b.activation_date)) as iso_leader_first_month,

concat(year(b.activation_date),'-',week(b.activation_date)) as iso_leader_first_week,

activation_date,

v.tiendas_con_pedido_no_entregado,

sum(act_date_subtotal) as activation_ticket,

case

when activation_ticket < 10 then '_<10'

when activation_ticket between 10 and 20 then '10 to 20'

when activation_ticket between 20 and 50 then '20 to 50'

when activation_ticket between 50 and 100 then '50 to 100'

else '_>=100' end as activation_ticket_group,

-- 1st 7 days

count(distinct iff (_date between activation_date and date_trunc('week',activation_date+7)-


1,dynamo_customer_id,null)) as customers_7d,

count(distinct iff (div0(_7d_total,_7d_subtotal)>=0.95,dynamo_customer_id,null)) as


organic_customers_7d,

count(distinct iff (_7d_subtotal>=70,dynamo_customer_id,null)) as customers_7d_70,

sum( iff (_date between activation_date and date_trunc('week',activation_date+7)-1,gross_value,null))


as D7_subtotal,

sum( iff (_date between activation_date and date_trunc('week',activation_date+7)-1,net_value,null)) as


D7_total,
iff(D7_subtotal > activation_ticket, 1,0) as post_act_activity_w1,

(D7_subtotal-D7_total) as D7_coupon_burn,

case

when customers_7d <10 then concat(customers_7d,'b')

else '10+' end as d7_customer_group,

case

when customers_7d_70 <10 then concat(customers_7d_70,'b')

else '10+' end as d7_customer_70_group,

case

when D7_total >= 2500 then 0.1*D7_total

when D7_total >= 1125 and D7_total < 2500 then 0.09*D7_total

when D7_total >= 500 and D7_total < 1125 then 0.085*D7_total

when D7_total < 500 then 0.05*D7_total end as est_commission_7d,

case

when est_commission_7d <15 then '_<15'

when est_commission_7d >=15 and est_commission_7d <30 then '_15-30'

when est_commission_7d >=30 and est_commission_7d <45 then '_30-45'

when est_commission_7d >=45 and est_commission_7d <60 then '_45-60'

when est_commission_7d >=60 and est_commission_7d <75 then '_60-75'

when est_commission_7d >=75 and est_commission_7d <90 then '_75-90'

when est_commission_7d >=90 and est_commission_7d <105 then '_90-105'

when est_commission_7d >=105 and est_commission_7d <120 then '_105-120'

when est_commission_7d >=120 and est_commission_7d <135 then '_120-135'

when est_commission_7d >=135 and est_commission_7d <150 then '_135-150'

when est_commission_7d >=150 then '_150+'

end as est_commission_7d_group,

-- 1st 14 days
count(distinct iff (_date between activation_date and date_trunc('week',activation_date+14)-
1,dynamo_customer_id,null)) as customers_14d,

count(distinct iff (div0(_14d_total,_14d_subtotal)>=0.95,dynamo_customer_id,null)) as


organic_customers_14d,

count(distinct iff (_14d_subtotal>=70,dynamo_customer_id,null)) as customers_14d_70,

sum( iff (_date between activation_date and date_trunc('week',activation_date+14)-1,gross_value,null))


as D14_subtotal,

sum( iff (_date between activation_date and date_trunc('week',activation_date+14)-1,net_value,null))


as D14_total,

(D14_subtotal-D14_total) as D14_coupon_burn,

iff(D14_subtotal>D7_subtotal,1,0) as day_7_to_14_activity,

D14_subtotal-D7_subtotal as day_7_to_14_subtotal,

D14_total-D7_total as day_7_to_14_total,

iff(D14_subtotal > activation_ticket, 1,0) as post_act_activity_w2,

case

when customers_14d <10 then concat(customers_14d,'b')

else '10+' end as d14_customer_group,

case

when customers_14d_70 <10 then concat(customers_14d_70,'b')

else '10+' end as d14_customer_70_group,

case

when D14_total >= 5000 then 0.1*D14_total

when D14_total >= 2250 and D14_total < 5000 then 0.09*D14_total

when D14_total >= 1000 and D14_total < 2250 then 0.085*D14_total

when D14_total < 1000 then 0.05*D14_total end as est_commission_14d,

case

when est_commission_14d <15 then '_<15'

when est_commission_14d >=15 and est_commission_14d <30 then '_15-30'

when est_commission_14d >=30 and est_commission_14d <45 then '_30-45'


when est_commission_14d >=45 and est_commission_14d <60 then '_45-60'

when est_commission_14d >=60 and est_commission_14d <75 then '_60-75'

when est_commission_14d >=75 and est_commission_14d <90 then '_75-90'

when est_commission_14d >=90 and est_commission_14d <105 then '_90-105'

when est_commission_14d >=105 and est_commission_14d <120 then '_105-120'

when est_commission_14d >=120 and est_commission_14d <135 then '_120-135'

when est_commission_14d >=135 and est_commission_14d <150 then '_135-150'

when est_commission_14d >=150 then '_150+'

end as est_commission_14d_group,

-- 1st 21 days (3w)

count(distinct iff (_date between activation_date and date_trunc('week',activation_date+21)-


1,dynamo_customer_id,null)) as customers_21d,

count(distinct iff (div0(_21d_total,_21d_subtotal)>=0.95,dynamo_customer_id,null)) as


organic_customers_21d,

count(distinct iff (_21d_subtotal>=70,dynamo_customer_id,null)) as customers_21d_70,

sum( iff (_date between activation_date and date_trunc('week',activation_date+21)-1,gross_value,null))


as D21_subtotal,

sum( iff (_date between activation_date and date_trunc('week',activation_date+21)-1,net_value,null))


as D21_total,

(D21_subtotal-D21_total) as D21_coupon_burn,

iff(D21_subtotal>D14_subtotal,1,0) as day_14_to_21_activity,

D21_subtotal-D14_subtotal as day_14_to_21_subtotal,

D21_total-D14_total as day_14_to_21_total,

iff(D21_subtotal > activation_ticket, 1,0) as post_act_activity_w3,

iff(D21_subtotal>D7_subtotal,1,0) as post_w1_activity_w3,

case
when customers_21d <10 then concat(customers_21d,'b')

else '10b+' end as d21_customer_group,

case

when customers_21d_70 <10 then concat(customers_21d_70,'b')

else '10b+' end as d21_customer_70_group,

case

when (D21_total-D14_total) >= 2500 then 0.1*(D21_total-D14_total) + est_commission_14d

when (D21_total-D14_total) >= 1125 and (D21_total-D14_total) < 2500 then 0.09*(D21_total-
D14_total) + est_commission_14d

when (D21_total-D14_total) >= 500 and (D21_total-D14_total) < 1125 then 0.085*(D21_total-
D14_total) + est_commission_14d

when (D21_total-D14_total) < 500 then 0.05*(D21_total-D14_total)+est_commission_14d end as


est_commission_21d,

case

when est_commission_21d <15 then '_<15'

when est_commission_21d >=15 and est_commission_21d <30 then '_15-30'

when est_commission_21d >=30 and est_commission_21d <45 then '_30-45'

when est_commission_21d >=45 and est_commission_21d <60 then '_45-60'

when est_commission_21d >=60 and est_commission_21d <75 then '_60-75'

when est_commission_21d >=75 and est_commission_21d <90 then '_75-90'

when est_commission_21d >=90 and est_commission_21d <105 then '_90-105'

when est_commission_21d >=105 and est_commission_21d <120 then '_105-120'

when est_commission_21d >=120 and est_commission_21d <135 then '_120-135'

when est_commission_21d >=135 and est_commission_21d <150 then '_135-150'

when est_commission_21d >=150 then '_150+'

end as est_commission_21d_group,

-- 1st 28 days (4w)


count(distinct iff (_date between activation_date and date_trunc('week',activation_date+28)-
1,dynamo_customer_id,null)) as customers_28d,

count(distinct iff (div0(_28d_total,_28d_subtotal)>=0.95,dynamo_customer_id,null)) as


organic_customers_28d,

count(distinct iff (_28d_subtotal>=100,dynamo_customer_id,null)) as customers_28d_100,

sum( iff (_date between activation_date and date_trunc('week',activation_date+28)-1,gross_value,null))


as D28_subtotal,

sum( iff (_date between activation_date and date_trunc('week',activation_date+28)-1,net_value,null))


as D28_total,

(D28_subtotal-D28_total) as D28_coupon_burn,

iff(D28_subtotal>D21_subtotal,1,0) as day_21_to_28_activity,

D28_subtotal-D21_subtotal as day_21_to_28_subtotal,

D28_total-D21_total as day_21_to_28_total,

iff(D28_subtotal>D14_subtotal,1,0) as day_14_to_28_activity,

iff(D28_subtotal > activation_ticket, 1,0) as post_act_activity_w4,

iff(D28_subtotal>D7_subtotal,1,0) as post_w1_activity_w4,

case

when customers_28d <10 then concat(customers_28d,'b')

else '10b+' end as d28_customer_group,

case

when customers_28d_100 <10 then concat(customers_28d_100,'b')

else '10b+' end as d28_customer_100_group,

case

when (D28_total-D14_total) >= 5000 then 0.1*(D28_total-D14_total) + est_commission_14d

when (D28_total-D14_total) >= 2250 and (D28_total-D14_total) < 5000 then 0.09*(D28_total-
D14_total) + est_commission_14d

when (D28_total-D14_total) >= 1000 and (D28_total-D14_total) < 2250 then 0.085*(D28_total-
D14_total) + est_commission_14d

when (D28_total-D14_total) < 1000 then 0.05*(D28_total-D14_total)+est_commission_14d end as


est_commission_28d,

case
when est_commission_28d <15 then '_<15'

when est_commission_28d >=15 and est_commission_28d <30 then '_15-30'

when est_commission_28d >=30 and est_commission_28d <45 then '_30-45'

when est_commission_28d >=45 and est_commission_28d <60 then '_45-60'

when est_commission_28d >=60 and est_commission_28d <75 then '_60-75'

when est_commission_28d >=75 and est_commission_28d <90 then '_75-90'

when est_commission_28d >=90 and est_commission_28d <105 then '_90-105'

when est_commission_28d >=105 and est_commission_28d <120 then '_105-120'

when est_commission_28d >=120 and est_commission_28d <135 then '_120-135'

when est_commission_28d >=135 and est_commission_28d <150 then '_135-150'

when est_commission_28d >=150 then '_150+'

end as est_commission_28d_group,

-- Monthly Calendar

----0 to 30 (30D)

sum( iff (_date between (activation_date) and (activation_date+30),gross_value,null)) as


D0_to_30d_subtotal,

sum( iff (_date between (activation_date) and (activation_date+30),net_value,null)) as D0_to_30d_total,

iff(D0_to_30d_subtotal>0,1,0) as day_0_to_30_activity,

count(distinct iff (_date between (activation_date) and (activation_date+30),dynamo_customer_id,null))


as D0_to_30d_buyers,

case

when D0_to_30d_buyers <10 then concat(D0_to_30d_buyers,'b')

else '10+' end as _30d_customer_group,

case

when D0_to_30d_subtotal >= 4500 then 'top'

when D0_to_30d_subtotal >= 2000 and D0_to_30d_subtotal <4500 then 'mid'

when D0_to_30d_subtotal >= 1000 and D0_to_30d_subtotal <2000 then 'basic_potential'


when D0_to_30d_subtotal >= 500 and D0_to_30d_subtotal <1000 then 'basic_500'

when D0_to_30d_subtotal >= 300 and D0_to_30d_subtotal <500 then 'basic_300'

when D0_to_30d_subtotal >= 100 and D0_to_30d_subtotal <300 then 'basic_100'

else 'basic_less_than_100' end as _30d_tier,

----M0

sum( iff (_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date))),gross_value,null)) as m0_subtotal,

sum( iff (_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date))),net_value,null)) as m0_total,

iff(m0_subtotal>0,1,0) as m0_activity,

count(distinct iff (_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date))),dynamo_customer_id,null)) as m0_buyers,

count(distinct

iff ((_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

,dynamo_customer_id,null)) as m0_new_buyers,

case

when m0_buyers <10 then concat(m0_buyers,'b')

else '10+' end as m0_customer_group,

----M1

sum( iff (_date between dateadd('month',1,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',2,date_trunc('month',activation_date))),gross_value,null)) as m1_subtotal,

sum( iff (_date between dateadd('month',1,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',2,date_trunc('month',activation_date))),net_value,null)) as m1_total,
iff(m1_subtotal>0,1,0) as m1_activity,

count(distinct iff (_date between dateadd('month',1,date_trunc('month',activation_date)) and


dateadd('day',-1,dateadd('month',2,date_trunc('month',activation_date))),dynamo_customer_id,null))
as m1_buyers,

count(distinct

iff ((_date between dateadd('month',1,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',2,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

,dynamo_customer_id,null)) as m1_new_buyers,

case

when m1_buyers <10 then concat(m1_buyers,'b')

else '10+' end as m1_customer_group,

case

when m1_subtotal >= 4500 then 'top'

when m1_subtotal >= 2000 and m1_subtotal <4500 then 'mid'

when m1_subtotal >= 1000 and m1_subtotal <2000 then 'basic_potential'

when m1_subtotal >= 500 and m1_subtotal <1000 then 'basic_500'

when m1_subtotal >= 300 and m1_subtotal <500 then 'basic_300'

when m1_subtotal >= 100 and m1_subtotal <300 then 'basic_100'

else 'basic_less_than_100' end as m1_tier,

----M2

sum( iff (_date between dateadd('month',2,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',3,date_trunc('month',activation_date))),gross_value,null)) as m2_subtotal,

sum( iff (_date between dateadd('month',2,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',3,date_trunc('month',activation_date))),net_value,null)) as m2_total,

iff(m2_subtotal>0,1,0) as m2_activity,
count(distinct iff (_date between dateadd('month',2,date_trunc('month',activation_date)) and
dateadd('day',-1,dateadd('month',3,date_trunc('month',activation_date))),dynamo_customer_id,null))
as m2_buyers,

count(distinct

iff ((_date between dateadd('month',2,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',3,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

,dynamo_customer_id,null)) as m2_new_buyers,

case

when m2_buyers <10 then concat(m2_buyers,'b')

else '10+' end as m2_customer_group,

case

when m2_subtotal >= 4500 then 'top'

when m2_subtotal >= 2000 and m2_subtotal <4500 then 'mid'

when m2_subtotal >= 1000 and m2_subtotal <2000 then 'basic_potential'

when m2_subtotal >= 500 and m2_subtotal <1000 then 'basic_500'

when m2_subtotal >= 300 and m2_subtotal <500 then 'basic_300'

when m2_subtotal >= 100 and m2_subtotal <300 then 'basic_100'

else 'basic_less_than_100' end as m2_tier,

----M3

sum( iff (_date between dateadd('month',3,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',4,date_trunc('month',activation_date))),gross_value,null)) as m3_subtotal,

sum( iff (_date between dateadd('month',3,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',4,date_trunc('month',activation_date))),net_value,null)) as m3_total,

iff(m3_subtotal>0,1,0) as m3_activity,
count(distinct iff (_date between dateadd('month',3,date_trunc('month',activation_date)) and
dateadd('day',-1,dateadd('month',4,date_trunc('month',activation_date))),dynamo_customer_id,null))
as m3_buyers,

count(distinct

iff ((_date between dateadd('month',3,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',4,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

,dynamo_customer_id,null)) as m3_new_buyers,

case

when m3_buyers <10 then concat(m3_buyers,'b')

else '10+' end as m3_customer_group,

case

when m3_subtotal >= 4500 then 'top'

when m3_subtotal >= 2000 and m3_subtotal <4500 then 'mid'

when m3_subtotal >= 1000 and m3_subtotal <2000 then 'basic_potential'

when m3_subtotal >= 500 and m3_subtotal <1000 then 'basic_500'

when m3_subtotal >= 300 and m3_subtotal <500 then 'basic_300'

when m3_subtotal >= 100 and m3_subtotal <300 then 'basic_100'

else 'basic_less_than_100' end as m3_tier,

----M0 MTD

sum( iff ((_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date)))) and (mtd_flag=1),gross_value,null)) as
mtd_m0_subtotal,

sum( iff ((_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date)))) and (mtd_flag=1),net_value,null)) as
mtd_m0_total,
iff(mtd_m0_subtotal>0,1,0) as mtd_m0_activity,

count(distinct iff ((_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date)))) and
(mtd_flag=1),dynamo_customer_id,null)) as mtd_m0_buyers,

count(distinct

iff ((_date between (activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

and (mtd_flag=1)

,dynamo_customer_id,null)) as mtd_m0_new_buyers,

case

when mtd_m0_buyers <10 then concat(mtd_m0_buyers,'b')

else '10+' end as mtd_m0_customer_group,

----M1 MTD

sum( iff ((_date between dateadd('month',1,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',2,date_trunc('month',activation_date)))) and (mtd_flag=1),gross_value,null)) as
mtd_m1_subtotal,

sum( iff ((_date between dateadd('month',1,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',2,date_trunc('month',activation_date)))) and (mtd_flag=1),net_value,null)) as
mtd_m1_total,

iff(mtd_m1_subtotal>0,1,0) as mtd_m1_activity,

count(distinct iff ((_date between dateadd('month',1,date_trunc('month',activation_date)) and


dateadd('day',-1,dateadd('month',2,date_trunc('month',activation_date)))) and
(mtd_flag=1),dynamo_customer_id,null)) as mtd_m1_buyers,

count(distinct

iff ((_date between dateadd('month',1,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',2,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

and (mtd_flag=1)
,dynamo_customer_id,null)) as mtd_m1_new_buyers,

case

when mtd_m1_buyers <10 then concat(mtd_m1_buyers,'b')

else '10+' end as mtd_m1_customer_group,

case

when mtd_m1_subtotal >= 4500 then 'top'

when mtd_m1_subtotal >= 2000 and mtd_m1_subtotal <4500 then 'mid'

when mtd_m1_subtotal >= 1000 and mtd_m1_subtotal <2000 then 'basic_potential'

when mtd_m1_subtotal >= 500 and mtd_m1_subtotal <1000 then 'basic_500'

when mtd_m1_subtotal >= 300 and mtd_m1_subtotal <500 then 'basic_300'

when mtd_m1_subtotal >= 100 and mtd_m1_subtotal <300 then 'basic_100'

else 'basic_less_than_100' end as mtd_m1_tier,

----M2 MTD

sum( iff ((_date between dateadd('month',2,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',3,date_trunc('month',activation_date)))) and (mtd_flag=1),gross_value,null)) as
mtd_m2_subtotal,

sum( iff ((_date between dateadd('month',2,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',3,date_trunc('month',activation_date)))) and (mtd_flag=1),net_value,null)) as
mtd_m2_total,

iff(mtd_m2_subtotal>0,1,0) as mtd_m2_activity,

count(distinct iff ((_date between dateadd('month',2,date_trunc('month',activation_date)) and


dateadd('day',-1,dateadd('month',3,date_trunc('month',activation_date)))) and
(mtd_flag=1),dynamo_customer_id,null)) as mtd_m2_buyers,

count(distinct

iff ((_date between dateadd('month',2,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',3,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

and (mtd_flag=1)

,dynamo_customer_id,null)) as mtd_m2_new_buyers,
case

when mtd_m2_buyers <10 then concat(mtd_m2_buyers,'b')

else '10+' end as mtd_m2_customer_group,

case

when mtd_m2_subtotal >= 4500 then 'top'

when mtd_m2_subtotal >= 2000 and mtd_m2_subtotal <4500 then 'mid'

when mtd_m2_subtotal >= 1000 and mtd_m2_subtotal <2000 then 'basic_potential'

when mtd_m2_subtotal >= 500 and mtd_m2_subtotal <1000 then 'basic_500'

when mtd_m2_subtotal >= 300 and mtd_m2_subtotal <500 then 'basic_300'

when mtd_m2_subtotal >= 100 and mtd_m2_subtotal <300 then 'basic_100'

else 'basic_less_than_100' end as mtd_m2_tier,

----M3 MTD

sum( iff ((_date between dateadd('month',3,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',4,date_trunc('month',activation_date)))) and (mtd_flag=1),gross_value,null)) as
mtd_m3_subtotal,

sum( iff ((_date between dateadd('month',3,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',4,date_trunc('month',activation_date)))) and (mtd_flag=1),net_value,null)) as
mtd_m3_total,

iff(mtd_m3_subtotal>0,1,0) as mtd_m3_activity,

count(distinct iff ((_date between dateadd('month',3,date_trunc('month',activation_date)) and


dateadd('day',-1,dateadd('month',4,date_trunc('month',activation_date)))) and
(mtd_flag=1),dynamo_customer_id,null)) as mtd_m3_buyers,

count(distinct

iff ((_date between dateadd('month',3,date_trunc('month',activation_date)) and dateadd('day',-


1,dateadd('month',4,date_trunc('month',activation_date))))

and (_date between (customer_activation_date) and dateadd('day',-


1,dateadd('month',1,date_trunc('month',customer_activation_date))))

and (mtd_flag=1)

,dynamo_customer_id,null)) as mtd_m3_new_buyers,
case

when mtd_m3_buyers <10 then concat(mtd_m3_buyers,'b')

else '10+' end as mtd_m3_customer_group,

case

when mtd_m3_subtotal >= 4500 then 'top'

when mtd_m3_subtotal >= 2000 and mtd_m3_subtotal <4500 then 'mid'

when mtd_m3_subtotal >= 1000 and mtd_m3_subtotal <2000 then 'basic_potential'

when mtd_m3_subtotal >= 500 and mtd_m3_subtotal <1000 then 'basic_500'

when mtd_m3_subtotal >= 300 and mtd_m3_subtotal <500 then 'basic_300'

when mtd_m3_subtotal >= 100 and mtd_m3_subtotal <300 then 'basic_100'

else 'basic_less_than_100' end as mtd_m3_tier,

-- 1st 1 week

count(distinct iff (_date between activation_date and date_trunc('week',activation_date+7)-


1,dynamo_customer_id,null)) as customers_1w,

count(distinct iff (div0(_1w_total,_1w_subtotal)>=0.95,dynamo_customer_id,null)) as


organic_customers_1w,

count(distinct iff (_1w_subtotal>=70,dynamo_customer_id,null)) as customers_1w_70,

sum(iff (_date between activation_date and date_trunc('week',activation_date+7)-1,gross_value,null))


as W1_subtotal,

sum(iff (_date between activation_date and date_trunc('week',activation_date+7)-1,net_value,null)) as


W1_total,

(W1_subtotal-W1_total) as W1_coupon_burn,

case

when customers_1w <10 then concat(customers_1w,'b')

else '10b+' end as W1_customer_group,


case

when customers_1w_70 <10 then concat(customers_1w_70,'b')

else '10b+' end as W1_customer_70_group,

-- 1st 2 weeks

count(distinct iff (_date between activation_date and date_trunc('week',activation_date+14)-


1,dynamo_customer_id,null)) as customers_2w,

count(distinct iff (div0(_2w_total,_2w_subtotal)>=0.95,dynamo_customer_id,null)) as


organic_customers_2w,

count(distinct iff (_2w_subtotal>=70,dynamo_customer_id,null)) as customers_2w_70,

sum(iff (_date between activation_date and date_trunc('week',activation_date+14)-1,gross_value,null))


as W2_subtotal,

sum(iff (_date between activation_date and date_trunc('week',activation_date+14)-1,net_value,null)) as


W2_total,

(W2_subtotal-W2_total) as W2_coupon_burn,

iff(W2_subtotal>W1_subtotal,1,0) as w2_activity,

case

when customers_2w <10 then concat(customers_2w,'b')

else '10b+' end as W2_customer_group,

case

when customers_2w_70 <10 then concat(customers_2w_70,'b')

else '10b+' end as W2_customer_70_group,

-- 1st 3 weeks

count(distinct iff (_date between activation_date and date_trunc('week',activation_date+21)-


1,dynamo_customer_id,null)) as customers_3w,

count(distinct iff (div0(_3w_total,_3w_subtotal)>=0.95,dynamo_customer_id,null)) as


organic_customers_3w,

count(distinct iff (_3w_subtotal>=70,dynamo_customer_id,null)) as customers_3w_70,

sum(iff (_date between activation_date and date_trunc('week',activation_date+21)-1,gross_value,null))


as W3_subtotal,
sum(iff (_date between activation_date and date_trunc('week',activation_date+21)-1,net_value,null)) as
W3_total,

(W3_subtotal-W3_total) as W3_coupon_burn,

iff(W3_subtotal>W2_subtotal,1,0) as w3_activity,

case

when customers_3w <10 then concat(customers_3w,'b')

else '10b+' end as W3_customer_group,

case

when customers_3w_70 <10 then concat(customers_3w_70,'b')

else '10b+' end as W3_customer_70_group,

sum(iff (_date between date_trunc('week',activation_date+28) and


date_trunc('week',activation_date+56)-1,gross_value,null)) as w4_w8_gmv,

iff(w4_w8_gmv>0,1,0) as w4_w8_activity,

-- 1st week WTD

count(distinct iff (_date between date_trunc('week',activation_date) and


date_trunc('week',activation_date)+ dayofweekiso(current_date-1),dynamo_customer_id,null)) as
wtd_customers_1w,

count(distinct iff (div0(wtd_1w_total,wtd_1w_subtotal)>=0.95,dynamo_customer_id,null)) as


wtd_organic_customers_1w,

count(distinct iff (wtd_1w_subtotal>=70,dynamo_customer_id,null)) as wtd_customers_1w_70,

sum(iff (_date between date_trunc('week',activation_date) and date_trunc('week',activation_date)+


dayofweekiso(current_date-1),gross_value,null)) as wtd_W1_subtotal,

sum(iff (_date between date_trunc('week',activation_date) and date_trunc('week',activation_date)+


dayofweekiso(current_date-1),net_value,null)) as wtd_W1_total,

(wtd_W1_subtotal-wtd_W1_total) as wtd_W1_coupon_burn,

case

when wtd_customers_1w <10 then concat(wtd_customers_1w,'b')

when wtd_customers_1w is null then null


else '10b+' end as wtd_W1_customer_group,

case

when wtd_customers_1w_70 <10 then concat(wtd_customers_1w_70,'b')

when wtd_customers_1w_70 is null then null

else '10b+' end as wtd_W1_customer_70_group,

case

when wtd_W1_total >= 2500 then 0.1*D7_total

when wtd_W1_total >= 1125 and wtd_W1_total < 2500 then 0.09*wtd_W1_total

when wtd_W1_total >= 500 and wtd_W1_total < 1125 then 0.085*wtd_W1_total

when Wtd_W1_total < 500 then 0.05*wtd_W1_total end as wtd_est_commission_7d,

case

when wtd_est_commission_7d <15 then '_<15'

when wtd_est_commission_7d >=15 and wtd_est_commission_7d <30 then '_15-30'

when wtd_est_commission_7d >=30 and wtd_est_commission_7d <45 then '_30-45'

when wtd_est_commission_7d >=45 and wtd_est_commission_7d <60 then '_45-60'

when wtd_est_commission_7d >=60 and wtd_est_commission_7d <75 then '_60-75'

when wtd_est_commission_7d >=75 and wtd_est_commission_7d <90 then '_75-90'

when wtd_est_commission_7d >=90 and wtd_est_commission_7d <105 then '_90-105'

when wtd_est_commission_7d >=105 and wtd_est_commission_7d <120 then '_105-120'

when wtd_est_commission_7d >=120 and wtd_est_commission_7d <135 then '_120-135'

when wtd_est_commission_7d >=135 and wtd_est_commission_7d <150 then '_135-150'

when wtd_est_commission_7d >=150 then '_150+'

end as wtd_est_commission_7d_group,

-- 2nd week WTD


count(distinct iff (_date between date_trunc('week',activation_date) and
date_trunc('week',activation_date+7)+ dayofweekiso(current_date-1),dynamo_customer_id,null)) as
wtd_customers_2w,

count(distinct iff (div0(wtd_2w_total,wtd_2w_subtotal)>=0.95,dynamo_customer_id,null)) as


wtd_organic_customers_2w,

count(distinct iff (wtd_2w_subtotal>=70,dynamo_customer_id,null)) as wtd_customers_2w_70,

sum(iff (_date between date_trunc('week',activation_date) and date_trunc('week',activation_date+7)+


dayofweekiso(current_date-1),gross_value,null)) as wtd_W2_subtotal,

sum(iff (_date between date_trunc('week',activation_date) and date_trunc('week',activation_date+7)+


dayofweekiso(current_date-1),net_value,null)) as wtd_W2_total,

(wtd_W2_subtotal-wtd_W2_total) as wtd_W2_coupon_burn,

iff(wtd_W2_subtotal>W1_subtotal,1,0) as wtd_w2_activity,

case

when wtd_customers_2w <10 then concat(wtd_customers_2w,'b')

when wtd_customers_2w is null then null

else '10b+' end as wtd_W2_customer_group,

case

when wtd_customers_2w_70 <10 then concat(wtd_customers_2w_70,'b')

when wtd_customers_2w_70 is null then null

else '10b+' end as wtd_W2_customer_70_group,

case

when wtd_W2_total >= 5000 then 0.1*wtd_W2_total

when wtd_W2_total >= 2250 and wtd_W2_total < 5000 then 0.09*wtd_W2_total

when wtd_W2_total >= 1000 and wtd_W2_total < 2250 then 0.085*wtd_W2_total

when wtd_W2_total < 1000 then 0.05*wtd_W2_total end as wtd_est_commission_14d,

case

when wtd_est_commission_14d <15 then '_<15'

when wtd_est_commission_14d >=15 and wtd_est_commission_14d <30 then '_15-30'

when wtd_est_commission_14d >=30 and wtd_est_commission_14d <45 then '_30-45'


when wtd_est_commission_14d >=45 and wtd_est_commission_14d <60 then '_45-60'

when wtd_est_commission_14d >=60 and wtd_est_commission_14d <75 then '_60-75'

when wtd_est_commission_14d >=75 and wtd_est_commission_14d <90 then '_75-90'

when wtd_est_commission_14d >=90 and wtd_est_commission_14d <105 then '_90-105'

when wtd_est_commission_14d >=105 and wtd_est_commission_14d <120 then '_105-120'

when wtd_est_commission_14d >=120 and wtd_est_commission_14d <135 then '_120-135'

when wtd_est_commission_14d >=135 and wtd_est_commission_14d <150 then '_135-150'

when wtd_est_commission_14d >=150 then '_150+'

end as wtd_est_commission_14d_group,

-- 3rd week WTD

count(distinct iff (_date between date_trunc('week',activation_date) and


date_trunc('week',activation_date+14)+ dayofweekiso(current_date-1),dynamo_customer_id,null)) as
wtd_customers_3w,

count(distinct iff (div0(wtd_3w_total,wtd_3w_subtotal)>=0.95,dynamo_customer_id,null)) as


wtd_organic_customers_3w,

count(distinct iff (wtd_3w_subtotal>=70,dynamo_customer_id,null)) as wtd_customers_3w_70,

sum(iff (_date between date_trunc('week',activation_date) and date_trunc('week',activation_date+14)+


dayofweekiso(current_date-1),gross_value,null)) as wtd_W3_subtotal,

sum(iff (_date between date_trunc('week',activation_date) and date_trunc('week',activation_date+14)+


dayofweekiso(current_date-1),net_value,null)) as wtd_W3_total,

(wtd_W3_subtotal-wtd_W3_total) as wtd_W3_coupon_burn,

iff(wtd_W3_subtotal>W2_subtotal,1,0) as wtd_w3_activity,

case

when wtd_customers_3w <10 then concat(wtd_customers_3w,'b')

when wtd_customers_3w is null then null

else '10b+' end as wtd_W3_customer_group,

case

when wtd_customers_3w_70 <10 then concat(wtd_customers_3w_70,'b')

when wtd_customers_3w_70 is null then null


else '10b+' end as wtd_W3_customer_70_group,

case

when (wtd_W3_total-D14_total) >= 2500 then 0.1*(wtd_W3_total-D14_total) + est_commission_14d

when (wtd_W3_total-D14_total) >= 1125 and (wtd_W3_total-D14_total) < 2500 then


0.09*(wtd_W3_total-D14_total) + est_commission_14d

when (wtd_W3_total-D14_total) >= 500 and (wtd_W3_total-D14_total) < 1125 then


0.085*(wtd_W3_total-D14_total) + est_commission_14d

when (wtd_W3_total-D14_total) < 500 then 0.05*(wtd_W3_total-D14_total)+est_commission_14d


end as wtd_est_commission_21d,

case

when wtd_est_commission_21d <15 then '_<15'

when wtd_est_commission_21d >=15 and wtd_est_commission_21d <30 then '_15-30'

when wtd_est_commission_21d >=30 and wtd_est_commission_21d <45 then '_30-45'

when wtd_est_commission_21d >=45 and wtd_est_commission_21d <60 then '_45-60'

when wtd_est_commission_21d >=60 and wtd_est_commission_21d <75 then '_60-75'

when wtd_est_commission_21d >=75 and wtd_est_commission_21d <90 then '_75-90'

when wtd_est_commission_21d >=90 and wtd_est_commission_21d <105 then '_90-105'

when wtd_est_commission_21d >=105 and wtd_est_commission_21d <120 then '_105-120'

when wtd_est_commission_21d >=120 and wtd_est_commission_21d <135 then '_120-135'

when wtd_est_commission_21d >=135 and wtd_est_commission_21d <150 then '_135-150'

when wtd_est_commission_21d >=150 then '_150+'

end as wtd_est_commission_21d_group,

--MTD

count(distinct iff ( (_date between activation_date and (activation_date+30)) and


(mtd_flag=1),gross_value,null)) as mtd_subtotal,

count(distinct iff ( (_date between activation_date and (activation_date+30)) and


(mtd_flag=1),dynamo_customer_id,null)) as mtd_customers,

count(distinct iff ((_28d_subtotal>=70) and (mtd_flag=1) ,dynamo_customer_id,null)) as


mtd_customers_70,
count(distinct iff ((_28d_subtotal>=100) and (mtd_flag=1) ,dynamo_customer_id,null)) as
mtd_customers_100,

case

when mtd_customers <10 then concat(mtd_customers,'b')

when mtd_customers is null then null

else '10b+' end as mtd_customers_group,

case

when mtd_customers_70 <10 then concat(mtd_customers_70,'b')

when mtd_customers_70 is null then null

else '10b+' end as mtd_customers_70_group

--cruces

from

(select

*,

iff(_date=activation_date,gross_value,0) as act_date_subtotal,

iff(day(create_date_time_tz)<=iff(day(current_date-1)=0,31,day(current_date-1)),1,0) as mtd_flag,

iff(dayofweekiso(create_date_time_tz)<=dayofweekiso(current_date-1),1,0) as wtd_flag,

--semana a corte domingo

sum(iff(_date between activation_date and date_trunc('week',activation_date+7)-1,gross_value,0)) over


(partition by dynamo_customer_id,dynamo_leader_id) as _1w_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date+7)-1,net_value,0)) over


(partition by dynamo_customer_id,dynamo_leader_id) as _1w_total,

sum(iff(_date between activation_date and date_trunc('week',activation_date+14)-1,gross_value,0))


over (partition by dynamo_customer_id,dynamo_leader_id) as _2w_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date+14)-1,net_value,0)) over


(partition by dynamo_customer_id,dynamo_leader_id) as _2w_total,

sum(iff(_date between activation_date and date_trunc('week',activation_date+21)-1,gross_value,0))


over (partition by dynamo_customer_id,dynamo_leader_id) as _3w_subtotal,
sum(iff(_date between activation_date and date_trunc('week',activation_date+21)-1,net_value,0)) over
(partition by dynamo_customer_id,dynamo_leader_id) as _3w_total,

--semana wtd

sum(iff(_date between activation_date and date_trunc('week',activation_date)


+dayofweekiso(current_date-1) ,gross_value,0)) over (partition by
dynamo_customer_id,dynamo_leader_id) as wtd_1w_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date)


+dayofweekiso(current_date-1) ,net_value,0)) over (partition by
dynamo_customer_id,dynamo_leader_id) as wtd_1w_total,

sum(iff(_date between activation_date and date_trunc('week',activation_date+7)


+dayofweekiso(current_date-1) ,gross_value,0)) over (partition by
dynamo_customer_id,dynamo_leader_id) as wtd_2w_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date+7)


+dayofweekiso(current_date-1) ,net_value,0)) over (partition by
dynamo_customer_id,dynamo_leader_id) as wtd_2w_total,

sum(iff(_date between activation_date and date_trunc('week',activation_date+14)


+dayofweekiso(current_date-1) ,gross_value,0)) over (partition by
dynamo_customer_id,dynamo_leader_id) as wtd_3w_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date+14)


+dayofweekiso(current_date-1) ,net_value,0)) over (partition by
dynamo_customer_id,dynamo_leader_id) as wtd_3w_total,

--semana en dias

sum(iff(_date between activation_date and date_trunc('week',activation_date+7)-1,gross_value,0)) over


(partition by dynamo_customer_id,dynamo_leader_id) as _7d_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date+7)-1,net_value,0)) over


(partition by dynamo_customer_id,dynamo_leader_id) as _7d_total,

sum(iff(_date between activation_date and date_trunc('week',activation_date+14)-1,gross_value,0))


over (partition by dynamo_customer_id,dynamo_leader_id) as _14d_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date+14)-1,net_value,0)) over


(partition by dynamo_customer_id,dynamo_leader_id) as _14d_total,

sum(iff(_date between activation_date and date_trunc('week',activation_date+21)-1,gross_value,0))


over (partition by dynamo_customer_id,dynamo_leader_id) as _21d_subtotal,

sum(iff(_date between activation_date and date_trunc('week',activation_date+21)-1,net_value,0)) over


(partition by dynamo_customer_id,dynamo_leader_id) as _21d_total,

sum(iff(_date between activation_date and date_trunc('week',activation_date+28)-1,gross_value,0))


over (partition by dynamo_customer_id,dynamo_leader_id) as _28d_subtotal,
sum(iff(_date between activation_date and date_trunc('week',activation_date+28)-1,net_value,0)) over
(partition by dynamo_customer_id,dynamo_leader_id) as _28d_total,

sum(iff(_date between (activation_date+30) and (activation_date+60),gross_value,0)) over (partition by


dynamo_customer_id,dynamo_leader_id) as _30d_to_60d_subtotal,

sum(iff(_date between (activation_date+30) and (activation_date+60),net_value,0)) over (partition by


dynamo_customer_id,dynamo_leader_id) as _30d_to_60d_total

from

(select *,

min(create_date_time_tz::date) over (partition by dynamo_leader_id) as activation_date,

min(create_date_time_tz::date) over (partition by dynamo_customer_id) as customer_activation_date,

date_trunc('month',activation_date) as activation_month,

date_trunc('month',customer_activation_date) as customer_activation_month,

date_trunc('month',create_date_time_tz::date) as _month,

create_date_time_tz::date as _date

from journey.base

where order_status not in ('CANCEL','OPEN') and country='PE'

--and store_url='kuntasshop'

--and (create_date_time_tz::date)>='2020-01-01'

)) b

left join journey_entrep je on je.dynamo_leader_id=b.dynamo_leader_id

left join (select distinct * from FAVODATA.SNP_SANDBOX.CT_ENTREP_GEO_ALLOCATION) g on


upper(g.city)=upper(coalesce(je.distrito,je.city))

left join vuupt as v on v.store_url=b.store_url

left join (select dynamo_leader_id,flag_mayorista, flag_coupon_fraud from


FAVODATA.SNP_UNTRUSTED.CT_GROWTH_MASTER_DB Group by 1,2,3) as flag

on flag.dynamo_leader_id=b.dynamo_leader_id

left join FAVODATA.SNP_UNTRUSTED.MAYORISTAS may on


may.dynamo_leader_id=b.dynamo_leader_id and may.leader_class<>'top_mayorista'

where may.dynamo_leader_id is null

and flag_mayorista=0 and flag_coupon_fraud=0

group by 1,2,3,4,5,6,7,8,9,10
)

panel_PMO

as

select

b.iso_leader_first_month,

b.iso_leader_first_week,

b.activation_ticket_group,

b.d7_customer_group,

b.d7_customer_70_group,

b.d14_customer_group,

b.d14_customer_70_group,

b.d21_customer_group,

b.d21_customer_70_group,

b.d28_customer_group,

b.d28_customer_100_group,

b.wtd_W1_customer_group,

b.wtd_W1_customer_70_group,

b.wtd_W2_customer_group,

b.wtd_W2_customer_70_group,

b.wtd_W3_customer_group,

b.wtd_W3_customer_70_group,

b.mtd_customers_group,

b.mtd_customers_70_group,

b.est_commission_7d_group,

b.est_commission_14d_group,
b.est_commission_21d_group,

b.est_commission_28d_group,

b.wtd_est_commission_7d_group,

b.wtd_est_commission_14d_group,

b.wtd_est_commission_21d_group,

b._30d_customer_group,

b._30d_tier,

count(distinct b.dynamo_leader_id) as n_entreps,

sum(b.tiendas_con_pedido_no_entregado) as no_entregados,

sum(b.post_act_activity_w1) as post_act_activity_w1,

sum(b.post_act_activity_w2) as post_act_activity_w2,

sum(b.post_act_activity_w3) as post_act_activity_w3,

sum(b.post_act_activity_w4) as post_act_activity_w4,

sum(b.D7_subtotal) as w1_subtotal,

sum(b.customers_7d) as w1_customers,

sum(b.D7_coupon_burn) as w1_coupon_burn,

sum(b.est_commission_7d) as est_commission_w1,

sum(b.day_7_to_14_activity) as w1_to_w2_activity,

sum(b.wtd_w2_activity) as wtd_w2_activity,

sum(b.D14_subtotal) as w2_subtotal,

sum(b.customers_14d) as w2_customers,

sum(b.D14_coupon_burn) as w2_coupon_burn,

sum(b.est_commission_14d) as est_commission_w2,

sum(b.D21_subtotal) as w3_subtotal,

sum(b.customers_21d) as w3_customers,

sum(b.D21_coupon_burn) as w3_coupon_burn,

sum(est_commission_21d) as est_commission_w3,

sum(b.day_14_to_21_activity) as w2_to_w3_activity,

sum(b.post_w1_activity_w3) as w1_to_w3_activity,
sum(b.wtd_w3_activity) as wtd_w3_activity,

sum(b.D28_subtotal) as w4_subtotal,

sum(b.customers_28d) as w4_customers,

sum(b.D28_coupon_burn) as w4_coupon_burn,

sum(b.est_commission_28d) as est_commission_w4,

sum(b.day_14_to_28_activity) as w3_to_w4_activity,

sum(b.post_w1_activity_w4) as w1_to_w4_activity,

sum(b.wtd_W1_subtotal) as wtd_W1_subtotal,

sum(b.wtd_customers_1w) as wtd_W1_customers,

sum(b.wtd_W1_coupon_burn) as wtd_W1_coupon_burn,

sum(b.wtd_est_commission_7d) as wtd_est_commission_7d,

sum(b.wtd_W2_subtotal) as wtd_W2_subtotal,

sum(b.wtd_customers_2w) as wtd_W2_customers,

sum(b.wtd_W2_coupon_burn) as wtd_W2_coupon_burn,

sum(b.wtd_est_commission_14d) as wtd_est_commission_14d,

sum(b.wtd_W3_subtotal) as wtd_W3_subtotal,

sum(b.wtd_customers_3w) as wtd_W3_customers,

sum(b.wtd_W3_coupon_burn) as wtd_W3_coupon_burn,

sum(b.wtd_est_commission_21d) as wtd_est_commission_21d,

sum(b.mtd_subtotal) as mtd_subtotal,

sum(b.mtd_customers) as mtd_customers,

sum(b.mtd_customers_70) as mtd_customers_70 ,

sum(b.D0_to_30d_subtotal) as _D0_to_30d_subtotal,

sum(b.D0_to_30d_total) as _D0_to_30d_total,

sum(b.D0_to_30d_buyers) as _D0_to_30d_buyers

from base as b

where b.activation_date>='2022-01-01'

group by 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28

)
,

entrep_detail

as

select

b.iso_leader_first_month,

b.iso_leader_first_week,

b.activation_date,

b.activation_ticket,

b.tiendas_con_pedido_no_entregado,

b.store_url,

b.leader_name,

b.phone_number,

b.dynamo_leader_id,

b.post_act_activity_w1,

b.customers_1w,

b.customers_1w_70,

b.W1_subtotal,

b.W1_total,

b.W1_coupon_burn,

b.customers_2w,

b.customers_2w_70,

b.W2_subtotal,

b.W2_total,

b.W2_coupon_burn,

b.w2_activity,

b.customers_3w,

b.customers_3w_70,
b.W3_subtotal,

b.W3_total,

b.W3_coupon_burn,

b.w3_activity,

b.wtd_customers_1w,

b.wtd_W1_subtotal,

b.wtd_W1_coupon_burn,

b.wtd_customers_2w,

b.wtd_W2_subtotal,

b.wtd_W2_coupon_burn,

b.mtd_subtotal,

b.mtd_customers,

b.mtd_customers_70,

b.mtd_customers_100,

b.w4_w8_activity

from base as b

where b.activation_date >='2022-12-01'

panel_monthly

as

select

b.iso_leader_first_month,

b.iso_leader_first_week,

b.m0_customer_group,

b.m1_customer_group,

b.m1_tier,

b.m2_customer_group,
b.m2_tier,

b.m3_customer_group,

b.m3_tier,

b.mtd_m0_customer_group,

b.mtd_m1_customer_group,

b.mtd_m1_tier,

b.mtd_m2_customer_group,

b.mtd_m2_tier,

b.mtd_m3_customer_group,

b.mtd_m3_tier,

b._30d_customer_group,

b._30d_tier,

b.customers_3w_70,

count(distinct b.dynamo_leader_id) as n_entreps,

sum(b.m0_subtotal) as m0_subtotal,

sum(b.m0_total) as m0_total,

sum(b.m0_activity) as m0_activity,

sum(b.m0_buyers) as m0_buyers,

sum(b.m0_new_buyers) as m0_new_buyers,

sum(b.m1_subtotal) as m1_subtotal,

sum(b.m1_total) as m1_total,

sum(b.m1_activity) as m1_activity,

sum(b.m1_buyers) as m1_buyers,

sum(b.m1_new_buyers) as m1_new_buyers,

sum(b.m2_subtotal) as m2_subtotal,

sum(b.m2_total) as m2_total,

sum(b.m2_activity) as m2_activity,

sum(b.m2_buyers) as m2_buyers,

sum(b.m2_new_buyers) as m2_new_buyers,
sum(b.m3_subtotal) as m3_subtotal,

sum(b.m3_total) as m3_total,

sum(b.m3_activity) as m3_activity,

sum(b.m3_buyers) as m3_buyers,

sum(b.m3_new_buyers) as m3_new_buyers,

sum(b.mtd_m0_subtotal) as mtd_m0_subtotal,

sum(b.mtd_m0_total) as mtd_m0_total,

sum(b.mtd_m0_activity) as mtd_m0_activity,

sum(b.mtd_m0_buyers) as mtd_m0_buyers,

sum(b.mtd_m0_new_buyers) as mtd_m0_new_buyers,

sum(b.mtd_m1_subtotal) as mtd_m1_subtotal,

sum(b.mtd_m1_total) as mtd_m1_total,

sum(b.mtd_m1_activity) as mtd_m1_activity,

sum(b.mtd_m1_buyers) as mtd_m1_buyers,

sum(b.mtd_m1_new_buyers) as mtd_m1_new_buyers,

sum(b.mtd_m2_subtotal) as mtd_m2_subtotal,

sum(b.mtd_m2_total) as mtd_m2_total,

sum(b.mtd_m2_activity) as mtd_m2_activity,

sum(b.mtd_m2_buyers) as mtd_m2_buyers,

sum(b.mtd_m2_new_buyers) as mtd_m2_new_buyers,

sum(b.mtd_m3_subtotal) as mtd_m3_subtotal,

sum(b.mtd_m3_total) as mtd_m3_total,

sum(b.mtd_m3_activity) as mtd_m3_activity,

sum(b.mtd_m3_buyers) as mtd_m3_buyers,

sum(b.mtd_m3_new_buyers) as mtd_m3_new_buyers

from base as b

where b.activation_date>='2022-01-01'

Group by 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19

)
-- For panel summary:

--select * from panel_pmo

--select * from panel_monthly

-- For detailed view:

select * from entrep_detail

--select * from base

You might also like