-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2B Case Study Cohort Analysis.sql
46 lines (41 loc) · 1.28 KB
/
2B Case Study Cohort Analysis.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
USE Northwind;
--- Customer id, find first time order (month)
WITH FIRSTBUY AS(
SELECT o.CustomerID,
DATEPART(MONTH, MIN(o.OrderDate)) first_time_buy
FROM Orders o
WHERE YEAR(o.OrderDate) = 1997
GROUP BY o.CustomerID),
--- Customer id, find all time order (month)
NEXTPURCHASE AS(
SELECT o.CustomerID,
DATEPART(MONTH, o.OrderDate) - first_time_buy AS buy_interval
FROM Orders o
JOIN FIRSTBUY f ON o.CustomerID = f.CustomerID
WHERE YEAR(o.OrderDate) = 1997),
--- Calculate the number of total distinct customer
INITIALUSER AS(
SELECT first_time_buy,
COUNT(DISTINCT CustomerID) AS users
FROM FIRSTBUY
GROUP BY first_time_buy),
--- Calculate the retention for each first time buy & buy interval
RETENTION AS(
SELECT
fb.first_time_buy,
buy_interval,
COUNT(DISTINCT np.CustomerID) AS users_transacting
FROM FIRSTBUY fb
JOIN NEXTPURCHASE np ON fb.CustomerID = np.CustomerID
WHERE buy_interval IS NOT NULL
GROUP BY fb.first_time_buy, buy_interval)
--- Convert the retention
SELECT
r.first_time_buy,
iu.users,
r.buy_interval,
r.users_transacting,
100.0*r.users_transacting/iu.users AS '%UserTransaction'
FROM RETENTION r
LEFT JOIN INITIALUSER iu ON r.first_time_buy = iu.first_time_buy
ORDER BY r.first_time_buy, r.buy_interval;