-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQL_3.sql
More file actions
88 lines (70 loc) · 1.7 KB
/
SQL_3.sql
File metadata and controls
88 lines (70 loc) · 1.7 KB
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
/* PROJET 5
CREATION DE NOUVELLES FEATURES
*/
/*
Définir une table RFM qui en plus référence :
- les catégories de produits
- les vendeurs
- les scores de satisfaction
- la localisation des consommateurs
*/
with time_table as (
select
max (order_purchase_timestamp) as last_date
from orders
),
table_customers as (
select
customer_id,
customer_state,
max (order_purchase_timestamp) over (partition by customer_id) as last_sale,
order_id
from orders
left join customers
using (customer_id)
),
table_commandes as(
select
customer_id,
customer_state,
last_sale,
order_id,
review_score
from table_customers
left join order_reviews
using (order_id)
),
rfm_table as(
select
customer_id,
customer_state,
avg(review_score) as score_moy,
count(order_id) as nb_commandes,
sum(price) as total_commandes,
(select julianday (last_date) from time_table) - julianday (max (last_sale)) as recence_commande,
product_id,
seller_id
from table_commandes
join order_items
using (order_id)
group by customer_id
),
table_finale as(
select
customer_id,
customer_state,
score_moy,
nb_commandes,
total_commandes,
recence_commande,
ntile(5) over (order by recence_commande asc) as R_score,
ntile(5) over (order by nb_commandes asc) as F_score,
ntile(5) over (order by total_commandes asc) as M_score,
product_category_name,
seller_id
from rfm_table
join products
using (product_id)
)
select *
from table_finale