如何在用户保留率图表中计算总数的百分比

时间:2019-09-06 12:00:24

标签: sql google-bigquery

我正在尝试建立用户每周保留图表。我有表格中的数据

count weeks_from_first_login
100   0
50    1
20    2
10    3
etc 

我想添加第三列,该列将为我提供在特定一周内登录的用户份额:

count weeks_from_first_login share
100   0                     100%
50    1                     50%
20    2                     20% 
etc

我该怎么做?如果有帮助,则日期原本为格式

device_id start_time etc
23123123  2019-08-20 07:01:17.185 UTC etc

我已经完成了

select firstlogin
, logindate
, round(timestamp_diff(logindate, firstlogin,day)/7) as weeks_from_first_login
, id
, app_id from (
select first_value(start_time) 
   over(partition by device_id 
   order by start_time asc) 
   as firstlogin
, device_id
, app_id
, start_time 
, timestamp_trunc(start_time , day) as logindate 
, device_id as id
  from `sessions.sessions` 
)

将数据保存到当前表单。

1 个答案:

答案 0 :(得分:0)

我不确定您的示例查询与您的问题有什么关系。但是根据您问题中的数据,您可以使用:

select weeks_from_first_login, count,
       count / max(count) over () as ratio
from t;

这假设最大数量是第一个。您还可以使用first_value()或条件聚合:

select weeks_from_first_login, count,
       count / first_value(count) over (order by weeks_from_first_login) as ratio
from t;