[英]SQL calculate bounce rate
我有以下 SQL 表(Postgres):
+-----------+------------+-------+
| SessionID | Received | Event |
+-----------+------------+-------+
| 1 | 1595207019 | visit |
| 1 | 1595207020 | play |
| 2 | 1595207040 | visit |
| 1 | 1595207050 | click |
+-----------+------------+-------+
我想计算跳出率,其中跳出被定义为访问事件,之后没有任何其他具有相同 session id 的事件。
嗯。 . . 我认为您想通过session_id
进行总结并计算访问类型。 然后聚合。 您的问题不是 100% 清楚,但我认为:
select (count(*) filter (where num_notvisits = 0) * 1.0 / count(*)) as bounce_rate
from (select session_id,
count(*) filter (where event = 'visit') as num_visits,
count(*) filter (where event <> 'visit') as num_notvisits
from t
group by session_id
) s
where num_visits > 0;
这是访问和非访问事件的会话数除以访问的会话数的比率。
实际上,您可以更简单地将外部 select 表述为:
select avg( (num_notvisits = 0)::int ) as bounce_rate
您可以像这样显式查询“反弹”的数量:
select count(*)
from t as t1
where t1.event = 'visit'
and not exists (select * from t as t2 where t1.received < t2.received and t1.sessionid = t2.sessionid)
不确定“跳出率”的分母具体是什么? 每个 session 的反弹? # 反弹/# 事件?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.