问题:用 postgresql 计算跳出数

在此处输入图像描述

我正在尝试使用 postgresql 计算有机反弹的数量。我想计算用户访问 site.com 并在查看第一页后离开的所有实例(例如,用户 ID 1 的第 4,5 行和第 6 行。与用户 ID 1 进入的第 1-3 行相反来自谷歌并访问了另外 2 个 site.com 页面。)

正确的答案是用户 ID 1 弹跳了 3 次,而用户 ID 2 根本没有弹跳。我相信 row_number() 和 partition by 可以用来解决这个问题。任何帮助构建 postgressql 查询将不胜感激。

编辑-这里是指向带有模式和数据http://sqlfiddle.com/#!12/39067的 jfiddle 的链接。

解答

问题从一个速率开始,但随后变为_正确答案将是用户 ID 1 被弹跳 3 次而用户 ID 2 根本没有弹跳_所以我正在回答正确答案,这是朝着速率方向迈出的一步.

SQL 小提琴

select user_id, count(c = 1 or null)
from (
    select user_id, g, count(*) c
    from (
        select *,
            count(referring_url != 'site.com' or null)
            over (partition by user_id order by datetime) g
        from t
    ) s
    group by user_id, g
) s
group by user_id;
 user_id | count 
---------+-------
       1 |     3
       2 |     0

如果您只想计算搜索引擎,那么:

count(referring_url in (
    'google.com', 'bing.com', 'ask.com', 'yahoo.com'
    ) or null)
Logo

PostgreSQL社区为您提供最前沿的新闻资讯和知识内容

更多推荐