-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2021.01.15.sql
141 lines (124 loc) · 4.45 KB
/
2021.01.15.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
132
133
134
135
136
137
138
139
140
141
CREATE TABLE subscription_history (user_id BIGINT NOT NULL,
product_id INT NOT NULL,
is_active BOOLEAN NOT NULL,
ts TIMESTAMP NOT NULL);
INSERT INTO subscription_history (user_id, product_id, is_active, ts)
SELECT a.n AS user_id,
round(random() * 3) AS product_id,
CASE
WHEN random() < 0.5 THEN FALSE
ELSE TRUE
END AS is_active,
TIMESTAMP '2017-03-01' + random() * interval '2 years' AS ts
FROM generate_series(1, 500000) AS a(n),
generate_series(1, 5);
SELECT count(*)
FROM subscription_history;
SELECT *
FROM subscription_history
ORDER BY user_id,
ts
LIMIT 1000;
SELECT a.user_id,
a.product_id,
MAX(a.ts) AS last_event_ts,
(SELECT is_active
FROM subscription_history b
WHERE b.ts = MAX(a.ts)
AND b.user_id = a.user_id
AND b.product_id = a.product_id ) AS current_status
FROM subscription_history a
WHERE a.user_id < 5
GROUP BY a.user_id,
a.product_id;
SELECT a.user_id,
a.product_id,
a.is_active AS current_status
FROM subscription_history a
WHERE NOT exists
(SELECT 1
FROM subscription_history b
WHERE b.user_id = a.user_id
AND b.product_id = a.product_id
AND b.ts > a.ts )
ORDER BY a.user_id,
a.product_id;
SELECT q.product_id,
count(DISTINCT q.user_id) AS current_active_users
FROM
(SELECT a.user_id,
a.product_id,
a.is_active AS current_status
FROM subscription_history a
WHERE NOT exists
(SELECT 1
FROM subscription_history b
WHERE b.user_id = a.user_id
AND b.product_id = a.product_id
AND b.ts > a.ts )) q
WHERE q.current_status
GROUP BY q.product_id;
WITH history AS
(SELECT a.user_id,
a.product_id,
a.is_active AS current_status,
row_number() OVER (PARTITION BY a.user_id,
a.product_id
ORDER BY a.ts DESC) AS rn
FROM subscription_history a)
SELECT b.product_id,
count(DISTINCT b.user_id) AS current_active_users
FROM history b
WHERE b.rn = 1
AND b.current_status
GROUP BY b.product_id;
WITH history AS
(SELECT a.user_id ,
a.product_id ,
a.is_active AS current_status ,
ROW_NUMBER() OVER (PARTITION BY a.user_id,
a.product_id
ORDER BY a.ts DESC) AS rn ,
a.ts - LEAD(a.ts) OVER (PARTITION BY a.user_id,
a.product_id
ORDER BY a.ts DESC) AS time_since_last_subscription_change
FROM subscription_history a)
SELECT b.product_id ,
COUNT(DISTINCT b.user_id) AS currently_active_users ,
AVG(time_since_last_subscription_change) AS avg_time_since_last_subscription_change
FROM history b
WHERE b.rn = 1
AND b.current_status
GROUP BY b.product_id;
WITH history AS
(SELECT a.user_id ,
a.product_id ,
a.is_active AS current_status ,
ROW_NUMBER() OVER W AS rn ,
a.ts - LEAD(a.ts) OVER W AS time_since_last_subscription_change
FROM subscription_history a WINDOW W AS (PARTITION BY a.user_id,
a.product_id
ORDER BY a.ts DESC))
SELECT b.product_id ,
COUNT(DISTINCT b.user_id) AS currently_active_users ,
AVG(time_since_last_subscription_change) AS avg_time_since_last_subscription_change
FROM history b
WHERE b.rn = 1
AND b.current_status
GROUP BY b.product_id;
WITH history AS
(SELECT a.user_id ,
a.product_id ,
a.is_active AS current_status ,
ROW_NUMBER() OVER W AS rn ,
a.ts - LEAD(a.ts) OVER W AS time_since_last_subscription_change
FROM subscription_history a WINDOW W AS (PARTITION BY a.user_id,
a.product_id
ORDER BY a.ts DESC))
SELECT b.product_id ,
COUNT(DISTINCT b.user_id) AS currently_active_users ,
AVG(time_since_last_subscription_change) AS avg_time_since_last_subscription_change
FROM history b
WHERE b.rn = 1
AND b.current_status
GROUP BY b.product_id;