-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomer_analytics.sql
More file actions
114 lines (95 loc) · 2.38 KB
/
Customer_analytics.sql
File metadata and controls
114 lines (95 loc) · 2.38 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
-- 13. How many *unique customers* have placed orders?
-- 14. What is the *average number of orders per customer*?
-- 15. Who are the *top 10 customers by lifetime spend*?
-- 16. What percentage of total revenue comes from the *top 10% of customers*?
-- 17. How many customers are *repeat buyers*?
-- 18. What is the *average customer lifetime value (LTV)*?
-- 13
SELECT
COUNT(DISTINCT customer_id) AS total_unique_customers
FROM orders;
-- 14
SELECT
ROUND(
COUNT(order_id) / COUNT(DISTINCT customer_id),
2
) AS avg_orders_per_customer
FROM orders
WHERE order_status = 'delivered';
-- 15
with customer_total_spend as
(select
o.customer_id,
sum(oi.price+oi.freight_value) as total_spend
from orders o
join order_items oi
on o.order_id=oi.order_id
where o.order_status='delivered'
group by o.customer_id),
ranked_spend as(
select
customer_id,
total_spend,
rank() over(order by total_spend desc) as rnk
from customer_total_spend
)
select
customer_id,total_spend,rnk
from ranked_spend
where rnk<=10
order by total_spend desc;
-- 16
with customer_total_spend as
(select
o.customer_id,
sum(oi.price+oi.freight_value) as total_spend
from orders o
join order_items oi
on o.order_id=oi.order_id
where o.order_status='delivered'
group by o.customer_id),
ranked_spend as(
select
customer_id,
total_spend,
rank() over(order by total_spend desc) as rnk
from customer_total_spend
),
counts as(
select count(*) as total_customers
from ranked_spend
)
select
round((sum(case when rnk<=(select total_customers*0.1 from counts)
then total_spend else 0 end)/sum(total_spend))*100,2)
as top_10_customer_spend_percentage
from ranked_spend;
-- 17
SELECT
COUNT(*) AS repeat_customers
FROM (
SELECT
c.customer_unique_id,
COUNT(o.order_id) AS total_orders
FROM orders o
JOIN customers c
ON o.customer_id = c.customer_id
GROUP BY c.customer_unique_id
HAVING COUNT(o.order_id) >= 2
) AS repeat_buyers;
-- 18
WITH customer_ltv AS (
SELECT
c.customer_unique_id,
SUM(oi.price + oi.freight_value) AS lifetime_spend
FROM orders o
JOIN order_items oi
ON o.order_id = oi.order_id
JOIN customers c
ON o.customer_id = c.customer_id
WHERE o.order_status = 'delivered'
GROUP BY c.customer_unique_id
)
SELECT
ROUND(AVG(lifetime_spend), 2) AS avg_customer_ltv
FROM customer_ltv;