[英]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.