【问题标题】:Returning customers回头客
【发布时间】:2020-05-12 14:26:37
【问题描述】:

假设我有一个表 web.orders 有两列:
user_id(整数)用户的唯一标识符
created_at(整数)纪元时间戳

我想知道过去 7 天或上个月有多少回头客。

所以首先我想我列出了过去 7 天中所有唯一的 user_id -s,然后在表的前面部分搜索每个人。然后总结命中。

我检查了有关该主题的这两个问题,但没有运气将它们转换为对我有用:
Find number of repeating visitors in a month - PostgreSQL
PostgreSQL: Identifying return visitors based on date - joins or window functions?

请问有人对此有解决方案吗?

【问题讨论】:

    标签: sql postgresql group-by subquery


    【解决方案1】:

    你可以使用exists:

    select count(distinct o.user_id) no_returning_visitors
    from web.orders o
    where 
        created_at >= extract(epoch from date_trunc('month', current_date) - interval '7' day)
        and created_at < extract(epoch from date_trunc('month', current_date))
        and exists (
            select 1 
            from web.orders o1
            where 
                o1.user_id = o.user_id 
                and o1created_at < extract(epoch from date_trunc('month', current_date) - interval '7' day)
        )
    

    【讨论】:

    • 谢谢!我帮了很多忙!我正在研究它,但不明白为什么需要这一行:and created_at &lt; extract(epoch from date_trunc('month', current_date))您的解决方案中有错字:o1created_at
    • 也许我们应该在第一个和第三个条件中使用day而不是month,对吧?
    猜你喜欢
    • 1970-01-01
    • 2021-04-12
    • 2021-02-02
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2021-10-27
    相关资源
    最近更新 更多