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
|
with main_tab as (
select
f.timestamp::date as date,
user_id,
activity_type,
f.container_id as group_id,
u.department as user_department,
u.location as user_location,
(
select
string_agg(distinct("userId"), ',') as group_owners
from
jusers_groups_copy g
where
g.place_id = f.container_id
and state like 'owner'
) as group_owners
from
fact_activity f
inner join ref_user u
on f.user_id = u.id
inner join ref_group r
on r.container_id = f.container_id
where
f.container_type like '700'
and f.timestamp::date < to_date($2, 'YYYY-MM-DD')
group by
date, user_id, activity_type, group_id, user_department, user_location
order by
date, user_id, activity_type, group_id, user_department, user_location
) , actif_tab as (
select date, group_id, user_id,
(CASE WHEN contributing > 0 THEN 1 ELSE 0 END) as contributing,
(CASE WHEN partipating > 0 THEN 1 ELSE 0 END) as partipating
from (select
date, group_id, user_id,
SUM(CASE WHEN activity_type in ('AddAttachment', 'Bookmark', 'BookmarkUpdate', 'Create', 'CreateOutcome', 'Move', 'Rate', 'Update', 'UpdateOutcome') THEN 1 ELSE 0 END)
as contributing,
SUM (CASE WHEN activity_type in ('Acclaim', 'Apply', 'Approve', 'Comment', 'CommentUpdate', 'Endorse', 'EndorsementApproval', 'Follow', 'Like', 'Mention', 'Resolved','RSVP', 'Send', 'Share', 'Tag', 'Vote') THEN 1 ELSE 0 END)
as partipating
from
main_tab
group by
date, group_id, user_id)t
)
select
m.date, m.group_id, m.user_department, m.user_location, m.group_owners,
SUM(CASE WHEN a.contributing > 0 THEN 1 ELSE 0 END) as sum_contribution,
SUM(CASE WHEN a.contributing = 0 and a.partipating > 0 THEN 1 ELSE 0 END) as sum_partipating,
(
select
count(distinct(user_id))
from
main_tab m2
where
m2.date = m.date
group by
date
) as actif,
(
select
count(distinct(user_id))
from
main_tab m3
where
m3.date <= m.date
) as register
from
actif_tab a, main_tab m
where
m.date > to_date($1, 'YYYY-MM-DD')
and m.date = a.date and m.group_id = a.group_id and m.user_id = a.user_id
group by
m.date, m.group_id, m.user_department, m.user_location, m.group_owners
order by
m.date, m.group_id, m.user_department, m.user_location, m.group_owners |
Partager