sql - How to get average of views before clicking on advertisement? -


i've got 2 tables containing information viewing , clicking on advertisement each user:

clicks:

userid   action   time 123      c        2016-01-08 01:57:00 123      c        2016-01-11 03:17:12 200      c        2016-01-09 02:20:10 332      c        2016-01-12 07:07:07 

views:

userid   action   time 123      0        2016-01-07 01:33:00 123      0        2016-01-10 04:12:13 200      w        2016-01-08 02:20:10 332      0        2016-01-09 07:07:07 332      0        2016-01-10 07:07:07 332      0        2016-01-11 07:07:07 

i want know average of views before clicking on advertisement first time, second time etc. example above: user 123 clicked first time having 1 view before, user 200 also, , user 332 had 3 views before clicking first time. average first click (1+1+3)/3=1.67. user 123 clicked twice, after 2 views (in total).

the result want looks this:

nr_clicks  avg_views 1          1.67 2          2 ... 

the action column in views table can take different values (but not c), in clicks table c. trying use join subquery , average results didn't expected.

ok, take me time think 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     (   select userid, time time_c,           count(*) on (partition userid order time) clickcume   views) join  (   select userid, time time_v,          count(*) on (partition userid order time) viewcume   clicks) b    on (a.userid=b.userid) time_c>=time_v ) c group userid, clickcume ) d group clickcume 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -