-
Notifications
You must be signed in to change notification settings - Fork 0
/
EngagementTracking - Monthly Engagement
516 lines (511 loc) · 9.19 KB
/
EngagementTracking - Monthly Engagement
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
WITH employers_users AS(
SELECT
CAST(
associations."users" #>> '{participant}' AS uuid
) AS participant
FROM
associations,
users
WHERE
CAST(
associations.users #>> '{employer}' AS uuid
) = users.user_id
AND(users.display_name = <Parameters.Employer>)
),
completedWC AS(
SELECT DISTINCT
user_id,
e.start_date AS call_date
FROM
event_attendees ea
JOIN events e ON e. ID = ea.event_id
INNER JOIN employers_users e_u ON ea.user_id = e_u.participant
WHERE
e. TYPE = 'welcome_call'
AND e.status = 'completed'
AND e.deleted_at IS NULL
AND ea.deleted_at IS NULL
),
dataCoachingRaw AS(
SELECT DISTINCT
ea.user_id,
ev. TYPE,
ev.status,
ev.start_date AS DATE
FROM
events ev
JOIN event_attendees ea ON ev. ID = ea.event_id
JOIN employers_users eu ON eu.participant = ea.user_id,
completedWC
WHERE
(
ev. TYPE = 'coaching_session'
OR ev. TYPE ILIKE 'genetic_reveal_%'
OR ev. TYPE = 'program_orientation'
)
AND ev.status = 'completed'
AND completedWC.user_id = ea.user_id
AND ea.deleted_at IS NULL
AND ev.deleted_at IS NULL
),
dataCoachingMonth AS(
SELECT DISTINCT
user_id,
to_char(DATE, 'yyyy-mm') AS MONTH,
COUNT(TYPE) AS COUNT
FROM
dataCoachingRaw
WHERE
TYPE = 'coaching_session'
AND status = 'completed'
GROUP BY
user_id,
to_char(DATE, 'yyyy-mm')
ORDER BY
user_id
),
user_records_raw AS(
SELECT DISTINCT
records.user_id,
types. NAME AS type_name,
records.created_at AS DATE,
entities. NAME AS entity_name,
records.meta #>> '{source}' AS SOURCE
FROM
records,
types,
entities,
completedWC
WHERE
types. ID = records.type_id
AND entities. ID = records.entity_id
AND completedWC.user_id = records.user_id
AND(
types. NAME = 'food'
OR types. NAME = 'exercise'
OR types. NAME = 'water'
OR types. NAME = 'wellbeing'
OR(
records.entity_id =(
SELECT
ID
FROM
entities
WHERE
NAME = 'Weight'
)
)
OR records.entity_id =(
SELECT
ID
FROM
entities
WHERE
NAME = 'Challenge'
)
)
AND records.user_id IN(
SELECT
participant
FROM
employers_users
)
AND records.deleted_at IS NULL
),
user_records_red0 AS(
SELECT
user_id,
(
CASE
WHEN(
type_name = 'food'
OR type_name = 'water'
) THEN
'nutrition'
WHEN(entity_name = 'Weight') THEN
'weight'
WHEN(entity_name = 'Challenge') THEN
'challenge'
ELSE
type_name
END
) AS type_name,
DATE,
source
FROM
user_records_raw
WHERE
source is null OR source != 'assessment'
),
dataNutritionDay AS(
SELECT DISTINCT
user_id,
type_name,
to_date(
to_char(DATE, 'yyyy-mm-dd'),
'yyyy-mm-dd'
) AS DATE
FROM
user_records_red0
WHERE
type_name = 'nutrition'
),
dataNutritionMonth AS(
SELECT DISTINCT
user_id,
COUNT(type_name) AS COUNT,
to_char(DATE, 'yyyy-mm') AS MONTH
FROM
dataNutritionDay
WHERE
type_name = 'nutrition'
GROUP BY
user_id,
to_char(DATE, 'yyyy-mm')
ORDER BY
user_id
),
dataExerciseDay AS(
SELECT DISTINCT
user_id,
type_name,
to_date(
to_char(DATE, 'yyyy-mm-dd'),
'yyyy-mm-dd'
) AS DATE
FROM
user_records_red0
WHERE
type_name = 'exercise'
),
dataExerciseMonth AS(
SELECT DISTINCT
user_id,
COUNT(type_name) AS COUNT,
to_char(DATE, 'yyyy-mm') AS MONTH
FROM
dataExerciseDay
WHERE
type_name = 'exercise'
GROUP BY
user_id,
to_char(DATE, 'yyyy-mm')
ORDER BY
user_id
),
dataWeightDay AS(
SELECT DISTINCT
user_id,
type_name,
to_date(
to_char(DATE, 'yyyy-mm-dd'),
'yyyy-mm-dd'
) AS DATE
FROM
user_records_red0
WHERE
type_name = 'weight'
),
dataWeightMonth AS(
SELECT DISTINCT
user_id,
COUNT(type_name) AS COUNT,
to_char(DATE, 'yyyy-mm') AS MONTH
FROM
dataWeightDay
WHERE
type_name = 'weight'
GROUP BY
user_id,
to_char(DATE, 'yyyy-mm')
ORDER BY
user_id
),
emails AS(
SELECT DISTINCT
ON(ue.user_id) ue.user_id AS user_id,
ue.email AS email
FROM
completedWC au
JOIN user_emails ue ON au.user_id = ue.user_id
WHERE
ue.deleted_at IS NULL
ORDER BY
ue.user_id,
ue.created_at DESC
),
inspirators AS(
SELECT DISTINCT
ON(ue.user_id, inspirator) ue.user_id AS user_id,
r.meta #>> '{first_name}' AS firstName,
r.meta #>> '{last_name}' AS lastName,
ue.email AS email,
(
SELECT
display_name
FROM
users
WHERE
user_id =(A .users #>> '{coach}') :: uuid
) AS inspirator
FROM
completedWC au
JOIN user_emails ue ON au.user_id = ue.user_id
JOIN records r ON au.user_id = r.user_id
JOIN associations A ON au.user_id =(A .users #>> '{participant}') :: uuid
WHERE
r.entity_id IN(
SELECT
ID
FROM
entities
WHERE
NAME = 'Intake'
)
AND A . TYPE = 'coach:participant'
AND A .deleted_at IS NULL
AND r.deleted_at IS NULL
AND ue.deleted_at IS NULL
ORDER BY
ue.user_id,
inspirator,
ue.created_at DESC,
A .created_at DESC
),
"program_week" AS(
SELECT
ROW_NUMBER() OVER(ORDER BY DATE) AS ID,
DATE "start_date",
DATE + '7 days' :: INTERVAL "end_date"
FROM
generate_series(
'2015-10-06 00:00' :: TIMESTAMP,
now() :: TIMESTAMP,
'7 days'
) AS DATE
),
"raw_weight" AS(
SELECT
wr.user_id,
CAST(
wr.measure_data #>> '{weight,value}' AS FLOAT
) AS
VALUE
,
wr.measure_data #>> '{weight,unit}' AS unit,
wr.created_at,
wr.record_at,
to_char(wr.created_at, 'YYYY-ww') AS week,
wr.meta #>> '{source}' AS SOURCE
FROM
completedWC ps
LEFT JOIN records wr ON wr.user_id = ps.user_id
AND wr.entity_id =(
SELECT
ID
FROM
entities
WHERE
NAME = 'Weight')
AND wr.deleted_at IS NULL
),
"raw2" AS(
SELECT
pw. ID AS program_week,
pw.start_date,
pw.end_date,
SOURCE,
rw.user_id,
rw.record_at AS record_at,
rw.week,
rw.
VALUE
AS
VALUE
,
COUNT(rw. VALUE) OVER(PARTITION BY rw.user_id)
FROM
program_week pw
JOIN raw_weight rw ON rw.record_at >= pw.start_date
AND rw.record_at < pw.end_date
WHERE
source is null OR source != 'assessment'
ORDER BY
program_week ASC
),
"weight_week" AS(
SELECT
raw2.user_id,
COUNT,
VALUE
,
record_at,
program_week,
MIN(program_week) OVER(PARTITION BY raw2.user_id) AS baseline_weight_week,
MAX(program_week) OVER(PARTITION BY raw2.user_id) AS last_week,
SOURCE
FROM
"raw2"
ORDER BY
user_id,
program_week
),
"initial_weight" AS(
SELECT
user_id,
MAX(VALUE) OVER(PARTITION BY user_id) AS baseline_weight
FROM
weight_week
WHERE
program_week = baseline_weight_week
),
"week_min_weight" AS(
SELECT DISTINCT
ON(user_id, program_week)
user_id,
program_week,
MIN(VALUE) OVER(
PARTITION BY user_id,
program_week
) AS last_weight
FROM
weight_week
ORDER BY
user_id,
program_week
),
"data" AS(
SELECT DISTINCT
ON(
weight_week.user_id,
weight_week.program_week
) weight_week.user_id,
SOURCE,
COUNT,
VALUE
,
record_at,
baseline_weight_week,
weight_week.program_week,
last_week,
baseline_weight,
last_weight ,- baseline_weight * 0.07 AS "7%_goal",
(last_weight - baseline_weight) AS weight_change
FROM
weight_week
LEFT JOIN initial_weight ON initial_weight.user_id = weight_week.user_id
LEFT JOIN week_min_weight ON week_min_weight.user_id = weight_week.user_id
AND week_min_weight.program_week = weight_week.program_week
ORDER BY
weight_week.user_id,
weight_week.program_week
),
total_data AS(
SELECT
user_id,
COUNT,
SOURCE,
VALUE
,
record_at,
to_char(record_at, 'yyyy-mm') AS MONTH,
baseline_weight_week,
program_week,
baseline_weight,
last_weight,
weight_change,
last_week,
(
CASE
WHEN(last_week = baseline_weight_week) THEN
0
ELSE
(
weight_change /(last_week - baseline_weight_week)
)
END
) AS week_change_rate,
(
CASE
WHEN(
- weight_change >= 0.0013 * baseline_weight
) THEN
1
ELSE
0
END
) AS "trackingto7%",
(
CASE
WHEN(weight_change <= "7%_goal") THEN
1
ELSE
0
END
) AS "achieved7%"
FROM
DATA
WHERE
-- record_at < < Parameters .end_date >
-- and record_at >= < Parameters .start_date >
--and
user_id IN(SELECT user_id FROM completedWC)
ORDER BY
user_id,
program_week
),
dataTrackingMonth AS(
SELECT DISTINCT
ON(user_id, MONTH) user_id,
MONTH,
(
CASE
WHEN SUM("trackingto7%") > 0 THEN
TRUE
ELSE
FALSE
END
) AS tracking
FROM
total_data
GROUP BY
user_id,
MONTH
ORDER BY
user_id,
MONTH
)
SELECT DISTINCT
dm.user_id,
dm.month,
dcm.COUNT AS coaching_sessions,
dm.COUNT AS nutrition,
dem.COUNT AS exercise,
dwm.COUNT AS weight,
dtm.tracking AS "trackingTo7%",
ins.firstName,
ins.lastName,
ins.email,
ins.inspirator
FROM
dataNutritionMonth dm
JOIN inspirators ins ON dm.user_id = ins.user_id
LEFT JOIN dataCoachingMonth dcm ON dm.user_id = dcm.user_id and dm.month = dcm.month
LEFT JOIN dataExerciseMonth dem ON dm.user_id = dem.user_id and dm.month = dem.month
LEFT JOIN dataWeightMonth dwm ON dm.user_id = dwm.user_id and dm.month = dwm.month
LEFT JOIN dataTrackingMonth dtm ON dm.user_id = dtm.user_id and dm.month = dtm.month
GROUP BY
dm.user_id,
dm.month,
ins.firstName,
ins.lastName,
ins.email,
ins.inspirator,
dm. COUNT,
dcm. COUNT,
dem. COUNT,
dwm. COUNT,
dtm.tracking
ORDER BY
dm.user_id,
dm.month