Está en la página 1de 3

with eventos as (

select distinct
idcampanha
,campanha
,idcliente
,canal
,status
,event_type
,min(event_timestamp) event_timestamp
,pinpoint_app_id
,pinpoint_campaign_id

from
ar_track_events

where
((pinpoint_app_id = %(pinpoint_app_id)s
and pinpoint_campaign_id = %(pinpoint_campaign_id)s)
or idcampanha = %(campanha_id)s)

and date(event_timestamp) between %(agendamento)s and (%(agendamento)s + 30)

group by
idcampanha
,campanha
,idcliente
,canal
,status
,event_type
,pinpoint_app_id
,pinpoint_campaign_id
)

select * from (
-------------------- ENVIADOS --------------------
select
'1-ENVIO' as tipo -- Comunicações enviadas ('send')
,count(distinct te.idcliente) clientes -->
Conta de forma distinta a quantidade de Shoppers
,0 faturamento
,0 compras
from
eventos te

where
te.idcliente is not null --> o
campo cliente = notNull
and split_part(te.event_type, '.', 2) = 'send' --> no
campo te.event_type ele pegará a palavra 'send'

union

-------------------- ENTREGUES --------------------


select
'2-ENTREGA' as tipo -- Comunicações entregues ('send' + 'SUCCESS')
,count(distinct te.idcliente) clientes
,0 faturamento
,0 compras
from
eventos te

where
te.idcliente is not null
and split_part(te.event_type, '.', 2) = 'send'
and status = 'SUCCESS'

union

-------------------- ATIVADOS (ABERTOS) --------------------


select
'3-ATIVAÇÃO' as tipo -- Clientes ativados (ENTREGUES + 'tipo_ativacao')
,count(distinct te.idcliente) clientes
,0 faturamento
,0 compras
from
eventos te

where
te.idcliente is not null
and split_part(te.event_type, '.', 2) = %(ativacao)s -- ALTERAR
and (status = 'SUCCESS' or status is null)

union

-------------------- CONVERTIDOS (COMPRAS) --------------------


select
'4-CONVERSÃO' as tipo -- Clientes convertidos (ATIVADOS + compras)
,count(distinct zm.idcliente) clientes
,coalesce(sum(zm.valortotal), 0) faturamento
,count(distinct idfrequencia) compras
from
eventos te
join zoombox_movimentos zm on te.idcliente = zm.idcliente

where
dataconsumo >= event_timestamp
and date(dataconsumo) between %(data_min)s and %(data_max)s
and split_part(te.event_type, '.', 2) = %(ativacao)s -- ALTERAR
and te.idcliente is not null
and (status = 'SUCCESS' or status = 'DUPLICATE_ADDRESS' or status is null)

union

-------------------- FEED - GOSTEI --------------------


select
'5.1-GOSTEI' as tipo -- Clientes que gostaram do Feed
,count(distinct te.idcliente) clientes
,0 faturamento
,0 compras
from
eventos te

where
te.idcliente is not null
and split_part(te.event_type, '.', 2) = 'like' -- ALTERAR

union
select
'5.2-DESMARCOU GOSTEI' as tipo -- Clientes que desmarcaram "Gostei"
,count(distinct te.idcliente) clientes
,0 faturamento
,0 compras
from
eventos te

where
te.idcliente is not null
and split_part(te.event_type, '.', 2) = 'unlike' -- ALTERAR

union

-------------------- FEED - NÃO GOSTEI --------------------


select
'6-NÃO GOSTEI' as tipo -- Clientes que informaram não gostar do Feed
,count(distinct te.idcliente) clientes
,0 faturamento
,0 compras
from
eventos te

where
te.idcliente is not null
and split_part(te.event_type, '.', 2) = 'remove' -- ALTERAR

order by
tipo

También podría gustarte