-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEvaluate the loyalty of users
49 lines (44 loc) · 1.17 KB
/
Evaluate the loyalty of users
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
/* Do all of these users actually use AhaSlides? */
with t1 as (
SELECT
u.id as user_id,
count(u.id) AS total_presentation,
SUM(CASE
WHEN onlineCountTime IS NULL THEN 1
ELSE 0 END) AS presentation_not_run
FROM users u
Left join presentations p
on u.id = p.userid
group by 1
order by 3 desc)
select
sum(case when total_presentation > 0 and presentation_not_run = 0 then 1 end) total_user_active,
(count(user_id) - sum(case when total_presentation > 0 and presentation_not_run = 0 then 1 end)) as total_user_no_active
from t1
/* Are they loyal to us? */
WITH t1 AS (
SELECT
userid,
DATE(u.createdAt) AS created_date,
DATE(p.updatedAt) AS updated_date
FROM users u
LEFT JOIN presentations p
on u.id = p.userid
GROUP BY 1, 2, 3),
t2 AS (
SELECT
MAX(updated_date) as last_day_update
FROM t1),
T3 AS (
SELECT distinct
t1.userid as user_id,
CASE
WHEN DATEDIFF((select last_day_update from t2),t1.updated_date) <= 30 THEN 'loyal'
ELSE 'not_loyal'
END AS loyalty
FROM t1
ORDER BY 2)
SELECT
sum(case when loyalty = 'loyal' then 1 END) as total_loyal,
(count(user_id) - sum(case when loyalty = 'loyal' then 1 END)) as total_not_loyal
FROM t3