diff --git a/EngagementTracking - NotTracking (All) b/EngagementTracking - NotTracking (All) new file mode 100644 index 0000000..c7c7838 --- /dev/null +++ b/EngagementTracking - NotTracking (All) @@ -0,0 +1,615 @@ +/* Nutrition Not-Tracking */ + +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 = ) +), + 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 = 'water' + ) + 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' + 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 +),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 +), notTracking as ( + +SELECT + mp.user_id as user_id +FROM + dataNutritionWeek dnw + FULL OUTER JOIN completedWC mp ON dnw.user_id = mp.user_id +WHERE + dnw.COUNT is null) +SELECT nt.user_id, ins.firstName, ins.lastName, ins.email, ins.inspirator +FROM notTracking nt left join inspirators ins on nt.user_id = ins.user_id + +/* Exercise Not-Tracking */ + +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 = ) +), + 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 = 'exercise' + ) + AND records.user_id IN( + SELECT + participant + FROM + employers_users + ) + AND records.deleted_at is null +), + dataExerciseDay AS( + SELECT DISTINCT + user_id, + type_name, + to_date( + to_char(DATE, 'yyyy-mm-dd'), + 'yyyy-mm-dd' + ) AS DATE + FROM + user_records_raw + 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 +),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 +), notTracking as( + +SELECT + mp.user_id as user_id +FROM + dataExerciseWeek dew + FULL OUTER JOIN completedWC mp ON dew.user_id = mp.user_id +WHERE + dew.COUNT is null) +SELECT nt.user_id, ins.firstName, ins.lastName, ins.email, ins.inspirator +FROM notTracking nt left join inspirators ins on nt.user_id = ins.user_id + +/* Well-Being Not-Tracking */ + +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 = ) +), + 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 = 'wellbeing' + ) + AND records.user_id IN( + SELECT + participant + FROM + employers_users + ) + AND records.deleted_at is null +), + dataWellbeingDay AS( + SELECT DISTINCT + user_id, + type_name, + to_date( + to_char(DATE, 'yyyy-mm-dd'), + 'yyyy-mm-dd' + ) AS DATE + FROM + user_records_raw + 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 +),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 +), notTracking as ( + +SELECT + mp.user_id as user_id +FROM + dataWellbeingWeek dnw + FULL OUTER JOIN completedWC mp ON dnw.user_id = mp.user_id +WHERE + dnw.COUNT is null) +SELECT nt.user_id, ins.firstName, ins.lastName, ins.email, ins.inspirator +FROM notTracking nt left join inspirators ins on nt.user_id = ins.user_id + +/* Weight Not-Tracking */ + +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 = ) +), + 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( + records.entity_id =( + SELECT + ID + FROM + entities + WHERE + NAME = 'Weight' + ) AND records.meta #>> '{source}' = 'body_trace' + ) + 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(entity_name = 'Weight') THEN + 'weight' + ELSE + type_name + END + ) AS type_name, + DATE + FROM + user_records_raw +), + 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 +), notTracking as ( + +SELECT + mp.user_id as user_id +FROM + dataWeightWeek dnw + FULL OUTER JOIN completedWC mp ON dnw.user_id = mp.user_id +WHERE + dnw.COUNT is null) +SELECT nt.user_id, ins.firstName, ins.lastName, ins.email, ins.inspirator +FROM notTracking nt left join inspirators ins on nt.user_id = ins.user_id + +/* Coaching Not-Tracking */ + +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 = ) +), + 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 ev.deleted_at is null + AND ea.deleted_at is null +), + dataCoachingMonth AS( + SELECT DISTINCT + user_id, + to_char(DATE, 'yyyy-mm') AS MONTH, + COUNT(TYPE) AS COUNT, + ( + CASE + WHEN(COUNT(TYPE) >= 0) THEN + 1 + ELSE + 0 + END + ) AS CoachTracking, + ( + CASE + WHEN(COUNT(TYPE) >= 2) THEN + 1 + ELSE + 0 + END + ) AS CoachTrackingGood + + FROM + dataCoachingRaw + WHERE + TYPE = 'coaching_session' + AND status = 'completed' + 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 +), notTracking as ( +SELECT + mp.user_id as user_id +FROM + dataCoachingMonth dcm + FULL OUTER JOIN completedWC mp ON dcm.user_id = mp.user_id +WHERE + dcm.COUNT is null) +SELECT + nt.user_id, ins.firstName, ins.lastName, ins.email, ins.inspirator +FROM + notTracking nt left join inspirators ins on nt.user_id = ins.user_id