set @cixu:=0;set @lastpage:="";selectpagename,avg(rank) as avg_rnfrom(selectuid,pagename,case when @lastpage != pagrname and @id =uid then @cixu := @cixu 1 when @lastpage = pagename and @id =uid then @cixu:=@cixuelse @cixu:=0 end as rank,@lastpage := pagename(select distinct uid,pagenamefrom tsorder by uid,date )a)bgroup by pagename;
select
page_name,avg(rnk) as avg_rnk
from
(
select
user_id,
page_name,
rank()over(partition by user_id,page_name order by min(ts)) as rnk
from
(
select
user_id,
page_name,
min(ts) as min_ts
from events
group by user_id,page_name
)tmp1
group by user_id,page_name
)tmp2
group by page_name