-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create EngagementTracking - Weeklies
- Loading branch information
1 parent
f3ba7fc
commit 2d98c54
Showing
1 changed file
with
338 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,338 @@ | ||
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 | ||
), | ||
user_records_raw AS( | ||
SELECT DISTINCT | ||
records.user_id, | ||
types. NAME AS type_name, | ||
records.created_at AS DATE, | ||
entities. NAME AS entity_name | ||
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') AND records.meta #>> '{source}' = 'body_trace' | ||
) | ||
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 | ||
FROM | ||
user_records_raw | ||
), | ||
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' | ||
), | ||
dataNutritionWeek AS( | ||
SELECT DISTINCT | ||
user_id, | ||
COUNT(type_name) AS COUNT, | ||
to_char(DATE, 'yyyy-ww') AS week | ||
FROM | ||
dataNutritionDay | ||
WHERE | ||
type_name = 'nutrition' | ||
GROUP BY | ||
user_id, | ||
to_char(DATE, 'yyyy-ww') | ||
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' | ||
), | ||
dataExerciseWeek AS( | ||
SELECT DISTINCT | ||
user_id, | ||
COUNT(type_name) AS COUNT, | ||
to_char(DATE, 'yyyy-ww') AS week | ||
FROM | ||
dataExerciseDay | ||
WHERE | ||
type_name = 'exercise' | ||
GROUP BY | ||
user_id, | ||
to_char(DATE, 'yyyy-ww') | ||
ORDER BY | ||
user_id | ||
), | ||
dataWellbeingDay 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 = 'wellbeing' | ||
), | ||
dataWellbeingWeek AS( | ||
SELECT DISTINCT | ||
user_id, | ||
COUNT(type_name) AS COUNT, | ||
to_char(DATE, 'yyyy-ww') AS week | ||
FROM | ||
dataWellbeingDay | ||
WHERE | ||
type_name = 'wellbeing' | ||
GROUP BY | ||
user_id, | ||
to_char(DATE, 'yyyy-ww') | ||
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' | ||
), | ||
dataWeightWeek AS( | ||
SELECT DISTINCT | ||
user_id, | ||
COUNT(type_name) AS COUNT, | ||
to_char(DATE, 'yyyy-ww') AS week | ||
FROM | ||
dataWeightDay | ||
WHERE | ||
type_name = 'weight' | ||
GROUP BY | ||
user_id, | ||
to_char(DATE, 'yyyy-ww') | ||
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 | ||
), dataWeek AS( | ||
SELECT | ||
( | ||
CASE | ||
WHEN(nuW.user_id IS NOT NULL) THEN | ||
nuW.user_id | ||
WHEN(exW.user_id IS NOT NULL) THEN | ||
exW.user_id | ||
WHEN(wbW.user_id IS NOT NULL) THEN | ||
wbW.user_id | ||
ELSE | ||
wtW.user_id | ||
END | ||
) AS user_id, | ||
( | ||
CASE | ||
WHEN(nuW.week IS NOT NULL) THEN | ||
nuW.week | ||
WHEN(exW.week IS NOT NULL) THEN | ||
exW.week | ||
WHEN(wbW.week IS NOT NULL) THEN | ||
wbW.week | ||
ELSE | ||
wtW.week | ||
END | ||
) AS week, | ||
nuW. COUNT AS NutritionCountWeekly, | ||
( | ||
CASE | ||
WHEN(nuW.COUNT > 0) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS NutritionTrack, | ||
( | ||
CASE | ||
WHEN(nuW.COUNT >= 2) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS NutritionTrackGoal, | ||
exW. COUNT AS ExerciseCountWeekly, | ||
( | ||
CASE | ||
WHEN(exW.COUNT > 0) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS ExerciseTracking, | ||
( | ||
CASE | ||
WHEN(exW.COUNT >= 3) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS ExerciseTrackingGoal, | ||
wbW. COUNT AS WellbeingCountWeekly, | ||
( | ||
CASE | ||
WHEN(wbW.COUNT > 0) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS WellbeingTracking, | ||
( | ||
CASE | ||
WHEN(wbW.COUNT >= 1) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS WellbeingTrackingGoal, | ||
wtW. COUNT AS WeightCountWeekly, | ||
( | ||
CASE | ||
WHEN(wtW.COUNT > 0) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS WeightTracking, | ||
( | ||
CASE | ||
WHEN(wtW.COUNT >= 2) THEN | ||
1 | ||
ELSE | ||
0 | ||
END | ||
) AS WeightTrackingGoal | ||
FROM | ||
dataNutritionWeek nuW | ||
FULL OUTER JOIN dataExerciseWeek exW ON nuW.week = exW.week | ||
AND nuW.user_id = exW.user_id | ||
FULL OUTER JOIN dataWellbeingWeek wbW ON nuW.week = wbW.week | ||
AND nuW.user_id = wbW.user_id | ||
FULL OUTER JOIN dataWeightWeek wtW ON nuW.week = wtW.week | ||
AND nuW.user_id = wtW.user_id) | ||
|
||
SELECT | ||
dw.*, | ||
ins.firstName, | ||
ins.lastName, | ||
ins.email, | ||
ins.inspirator | ||
|
||
FROM dataWeek dw join inspirators ins on dw.user_id = ins.user_id |