I am trying to calculate the outflow of clients based on the activity that they could do, against the outflow by date, which is a normal thing. We have events related to a specific host, in my example all the events are posted by Alice, but these can be different hosts.
All people who follow a particular event should be placed in the category (new, active, whipped and resurrected).
New one . The first time a person follows an event from a specific host.
Active . Follow it (and the last event from a specific host was also executed).
Churned : The follower had the opportunity to follow, but did not.
Resurrected . The follower who hacked started following the previous host.
declare @events table (event varchar(50), host varchar(50), date date)
declare @eventFollows table (event varchar(50), follower varchar(50))
insert into @events values ('e_1', 'Alice', GETDATE())
insert into @events values ('e_2', 'Alice', GETDATE())
insert into @events values ('e_3', 'Alice', GETDATE())
insert into @events values ('e_4', 'Alice', GETDATE())
insert into @events values ('e_5', 'Alice', GETDATE())
insert into @eventFollows values ('e_1', 'Bob')
insert into @eventFollows values ('e_2', 'Bob')
insert into @eventFollows values ('e_4', 'Megan')
insert into @eventFollows values ('e_5', 'Bob')
insert into @eventFollows values ('e_5', 'Megan')
select * from @events
select * from @eventFollows
The expected result should be something like this
select 'e_1', 1 as New, 0 as resurrected, 0 as active, 0 as churned
union all
select 'e_2', 0 as New, 0 as resurrected, 1 as active, 0 as churned
union all
select 'e_3', 0 as New, 0 as resurrected, 0 as active, 1 as churned
union all
select 'e_4', 1 as New, 0 as resurrected, 0 as active, 0 as churned
union all
select 'e_5', 0 as New, 1 as resurrected, 1 as active, 0 as churned
I started by asking for something like below, but the problem is that I am not getting all the events that followers did not follow (but could follow).
select a.event, follower, date,
LAG (a.event,1) over (partition by a.host, ma.follower order by date) as lag,
LEAD (a.event,1) over (partition by a.host, ma.follower order by date) as lead,
LAG (a.event,1) over (partition by a.host order by date) as lagP,
LEAD (a.event,1) over (partition by a.host order by date) as leadP
from @events a left join @eventFollows ma on ma.event = a.event order by host, follower, date
Any ideas?