Skip to content

Commit

Permalink
Create AppUsage - App Usage Query
Browse files Browse the repository at this point in the history
  • Loading branch information
gmark-newt committed Mar 30, 2016
1 parent 29e1efb commit 8622a0b
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions AppUsage - App Usage Query
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
WITH employers_users AS(
SELECT
CAST(
associations."users" #>> '{participant}' AS uuid
) AS user_id
FROM
associations,
users
WHERE
CAST(
associations.users #>> '{employer}' AS uuid
) = users.user_id
AND(users.display_name = <Parameters.Employer>)
AND users.deleted_at is null
AND associations.deleted_at is null
),
completedPO AS(
SELECT DISTINCT
ea.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.user_id
WHERE
e.type = 'program_orientation' AND
e.status = 'completed' AND
e.deleted_at is null AND
ea.deleted_at is null
), appUsers AS (
SELECT DISTINCT
cpo.user_id
FROM
user_logs ul JOIN completedPO cpo ON ul.user_id = cpo.user_id
WHERE
ul.message ILIKE 'newtmobileapp navigation tracking%' AND
ul.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,
appUsers
WHERE
types. ID = records.type_id
AND entities. ID = records.entity_id
AND appUsers.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
user_id
FROM
employers_users
)
AND records.deleted_at is null
), dataMonth AS(
SELECT DISTINCT
user_id,
COUNT(*) AS COUNT,
to_char(DATE, 'yyyy-mm') AS month
FROM
user_records_raw
GROUP BY
user_id,
to_char(DATE, 'yyyy-mm')
ORDER BY
user_id
), 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 appUsers 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
)
select distinct
dm.user_id,
dm.COUNT,
dm.month,
ins.firstName,
ins.lastName,
ins.email,
ins.inspirator
from dataMonth dm FULL OUTER JOIN inspirators ins on dm.user_id = ins.user_id

0 comments on commit 8622a0b

Please sign in to comment.