我想根据他们本来可以完成的活动来计算客户流失率,而不是按日期流失,这是正常情况。我们有与特定主机连接的事件,在我的示例中,所有事件都由Alice托管,但可能是不同的主机。填补空白,基于事件
所有遵循特定事件的人都应被放置在一个类别中(新的,活跃的,搅动的和复活的)。
新:第一次一个人跟随来自特定主机的事件。
活动:再次关注(并且还遵循了来自特定主持人的最后一个活动)。
搅动:追随者有机会跟随,但没有。
复活:已经搅动的追随者已经开始遵循先前遵循的主持人。
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') --new
insert into @eventFollows values ('e_2', 'Bob') --active
--Bob churned
insert into @eventFollows values ('e_4', 'Megan') --new
insert into @eventFollows values ('e_5', 'Bob') --resurrected
insert into @eventFollows values ('e_5', 'Megan') --active
select * from @events
select * from @eventFollows
预期的结果应该是这样的
select 'e_1', 1 as New, 0 as resurrected, 0 as active, 0 as churned --First time Bob follows Alice event
union all
select 'e_2', 0 as New, 0 as resurrected, 1 as active, 0 as churned --Bob follows the next event that Alice host (considered as Active)
union all
select 'e_3', 0 as New, 0 as resurrected, 0 as active, 1 as churned --Bob churns since he does not follow the next event
union all
select 'e_4', 1 as New, 0 as resurrected, 0 as active, 0 as churned --First time Megan follows Alice event
union all
select 'e_5', 0 as New, 1 as resurrected, 1 as active, 0 as churned --Second time (active) for Megan and Bob is resurrected
我开始用类似下面的查询,但问题是,我不明白的是,追随者也没有的所有事件跟随(但可能已经跟随)。
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
任何想法?
“搅动”后会发生什么?他们搅拌一次还是停止搅动? – gbn
每个人或COUNT个人的标志是? – gbn
当你流失后,你可以重新复活,然后你可以再次搅动。在我的例子中,鲍勃搅动(即将离开事件3和事件4),但在事件5复活。 – corpat