How to get average of views before clicking on advertisement?
Ok, it take me some time but I think it works well:

select clickcume nr_clicks, avg(view_b) avg_view_before from(
select userid, clickcume, max(viewcume) view_b from(
select a.*, b.time_v, b.viewcume  from   
(
  select userid, time as time_c, 
         count(*) over (partition by userid order by time) as clickcume
  from views) a join 
(
  select userid, time as time_v,
         count(*) over (partition by userid order by time) as viewcume
  from clicks) b 
  on (a.userid=b.userid)
where time_c&gt;=time_v ) c
group by userid, clickcume ) d
group by clickcume