繁体   English   中英

计算postgresql中时间T的队列转化率

[英]Calculate cohort conversion rate at time T in postgresql

我有一个看起来像这样的表:

队列 注册日期 转换日期
一种 1 月 1 日 1 月 2 日
一种 2月1日 2月3日
一种 3月1日 3月4日
一种 4月1日 4月5日
5月1日 5月3日
6月1日 6月5日
7 月 1 日 7 月 7 日

我想在注册后一次获得特定队列的转化率 T。 所以表格应该是这个样子:

队列 注册后的天数 兑换率
一种 0 0.00
一种 1个 0.25
一种 2个 0.50
一种 3个 0.75
一种 4个 1.00
一种 5个 1.00
一种 6个 1.00
一种 7 1.00
0 0.00
1个 0.00
2个 0.33
3个 0.33
4个 0.66
5个 0.66
6个 1.00
7 1.00

您可以生成一系列数字并交叉连接所有 Gohorts 以获得有问题的项目。 现在你可以使用 window 函数了。 例如

样本数据

select * into tbl
from (
   values
     ('A', 1),
     ('A', 2),
     ('A', 3),
     ('A', 4),
     ('B', 2),
     ('B', 4),
     ('B', 6)
)t(Cohort, ndays);

查询

select s.s, c.Cohort
   , 1.0 * count(t.ndays) over(partition by c.Cohort order by s.s) / count(t.ndays) over(partition by c.Cohort) convertion 
from generate_series(0,7) s
cross join (select distinct Cohort from tbl) c
left join tbl t on t.Cohort = c.Cohort and t.ndays = s.s 
order by c.Cohort, s.s;

暂无
暂无

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

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