简体   繁体   English

sql计算保留

[英]sql calculate retention

how do I calculate retention rate in SQL to add as a column to my table 如何在SQL中计算保留率以添加为表中的列

  1. first day I had in Brazil 411 unique users 在巴西的第一天,就有411个唯一用户
  2. second day only 154 users came back (retention rate is 154/411) 第二天只有154位用户返回(保留率为154/411)
  3. third day only 115 came back again (115/154) 第三天,只有115人再次回来(115/154)
  4. last day only 81 out of 98 users came back 最后一天,在98位用户中,只有81位回来了

      Country Date sum_views distinct_user_ids Brazil 5/5/2016 3793 411 Brazil 5/6/2016 1632 154 Brazil 5/7/2016 1456 115 Brazil 5/8/2016 1223 98 Brazil 5/9/2016 993 81 Canada 5/5/2016 6419 708 Canada 5/6/2016 2649 235 Canada 5/7/2016 2578 197 Canada 5/8/2016 2024 151 Canada 5/9/2016 1893 141 United states 5/5/2016 13007 1438 United states 5/6/2016 5755 522 United states 5/7/2016 5502 419 United states 5/8/2016 4915 362 United states 5/9/2016 3713 284 

output : 输出:

a column with retention rate 具有保留率的列

You need the previous value for the denominator. 您需要分母的先前值。 You can get that with a correlated subquery: 您可以通过相关的子查询来获得它:

select t.*, tprev.distinct_user_ids,
       (t.distinct_user_ids / tprev.distinct_user_ids)
from t join
     t tprev
     on tprev.country = t.country and
        tprev.date = date(t.date, '-1 day');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM