-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOlist_Modelagem.sql
131 lines (116 loc) · 3.79 KB
/
Olist_Modelagem.sql
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
--------------------------------------------------------------------------------------------------------------------
# Olist_Funil de Marketing
--------------------------------------------------------------------------------------------------------------------
# criando banco de dados
Create database olist_mark;
use olist_mark;
# importando dados
create table df_mqk
(
first_contact_date date,
origin varchar(50) not null,
mql_id int not null auto_increment,
constraint pk_mql primary key (mql_id)
);
select * from df_mqk;
desc df_mqk;
create table df_close
(
won_date date,
business_segmento varchar(70),
lead_type varchar(70),
lead_behavior_profiel varchar(70),
business_type varchar(70),
id_close int not null auto_increment,
constraint pk_close primary key (id_close)
);
select * from df_close;
select * from df_mqk;
--------------------------------------------------------------------------------------------------------------------
# Modelagem
--------------------------------------------------------------------------------------------------------------------
# MQL
create table canal (id_canal int primary key auto_increment)
select distinct
origin
from df_mqk;
select * from canal;
create table MQL (id_mql int primary key auto_increment) -- Criando fato
select
origin,
first_contact_Date
from df_mqk;
select * from mql;
create table MQL_1 (id_mql int primary key auto_increment) -- add FK na fato
select
m.origin,
m.first_contact_Date,
id_tempo as id_dim_tempo,
id_canal as id_dim_canal
from mql m
join tempo_m t on t.first_contact_Date = m.first_contact_Date
join canal c on c.origin = m.origin;
select * from mql_1;
select origin, first_contact_Date, count(origin) from mql_1 group by 1,2;
-------------------------------------------
# Fechamento
-------------------------------------------
create table persona (id_persona int primary key auto_increment)
select distinct
business_segmento,
lead_behavior_profiel as lead_behavior,
business_type
from df_close
where business_segmento is not null;
select * from persona;
create table fechamento (id_close int primary key auto_increment) -- Criando fato
select
won_date,
business_segmento,
lead_behavior_profiel as lead_behavior,
business_type
from df_close;
select * from fechamento;
create table fechamento_1 (id_close int primary key auto_increment) -- add FK na fato
select
f.won_date,
f.business_segmento,
f.lead_behavior,
f.business_type,
id_tempo as id_dim_tempo,
id_persona as id_dim_canal
from fechamento f
join tempo_f t on t.won_date = f.won_Date
join persona p on p.business_segmento = f.business_segmento;
select * from mql_1;
-------------------------------------------
# Tempo
-------------------------------------------
create table tempo (id_tempo int primary key auto_increment)
select
won_date as tempo
from fechamento_1 f
union
select
first_contact_Date as tempo
from mql;
select * from tempo;
--------------------------------------------------------------------------------------------------------------------
# View
--------------------------------------------------------------------------------------------------------------------
create view olist_mark as
select
id_mql,
id_close,
origin,
tempo,
business_segmento,
lead_behavior,
business_type,
first_contact_Date,
won_date,
TIMESTAMPDIFF(day, won_date, first_contact_Date) as contato_primeiro_ate_fechamento
from fechamento_1 f
join tempo t on t.id_tempo = f.id_dim_tempo -- tab. fechamento e tempo conectado com tempo
join mql_1 q on q.id_dim_tempo = t.id_tempo; -- limitação dados de ate 6 meses
select * from olist_mark;