简体   繁体   English

SQL 计算跳出率

[英]SQL calculate bounce rate

I have the following SQL table (Postgres):我有以下 SQL 表(Postgres):

+-----------+------------+-------+
| SessionID |  Received  | Event |
+-----------+------------+-------+
|         1 | 1595207019 | visit |
|         1 | 1595207020 | play  |
|         2 | 1595207040 | visit |
|         1 | 1595207050 | click |
+-----------+------------+-------+

I want to calculate bounce rate where bounce is defined as visit event not followed by any other event with the same session id.我想计算跳出率,其中跳出被定义为访问事件,之后没有任何其他具有相同 session id 的事件。

Hmmm.嗯。 . . . . I think you want to summarize by session_id and count the types of visits.我认为您想通过session_id进行总结并计算访问类型。 then aggregate.然后聚合。 Your question is not 100% clear, but I thinK:您的问题不是 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;

This is the ratio of the number of sessions with both a visit and a non-visit event divided by the number of sessions with a visit.这是访问和非访问事件的会话数除以访问的会话数的比率。

You can actually phrase the outer select more simply as:实际上,您可以更简单地将外部 select 表述为:

select avg( (num_notvisits = 0)::int ) as bounce_rate

You can explicitly query for the number of "bounces" like this:您可以像这样显式查询“反弹”的数量:

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)

Not sure what is the denominator of the "bounce rate" specifically?不确定“跳出率”的分母具体是什么? Bounces per session?每个 session 的反弹? # bounces / # events? # 反弹/# 事件?

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

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