【问题标题】:Daily count of user_ids who have visited my store 4 or more than 4 times every day每天访问我的商店 4 次或超过 4 次的 user_id 的每日计数
【发布时间】:2021-12-10 23:28:15
【问题描述】:

我有一个访问过我平台的 user_id 表。我只想计算每个用户和每天访问我的商店 4 次或更多次的用户 ID,持续时间为 10 天。 为了实现这一点,我使用了这个查询:

select date(arrival_timestamp), count(user_id)
from mytable
where date(arrival_timestamp) >= current_date-10
and date(arrival_timestamp) < current_date
group by 1
having count(user_id)>=4
order by 2 desc
limit 10; 

但是此查询实际上是在获取计数值大于 4 的所有用户,而不是每天都包含几乎所有用户,因此我无法仅隔离那些在特定网站上多次访问我的商店的用户日。感谢您提供这方面的任何帮助。

谢谢

【问题讨论】:

  • 请以文本形式提供数据样本(最好作为数据库小提琴中的插入语句)和确切的预期结果。

标签: sql postgresql group-by count aggregate-functions


【解决方案1】:

你可以试试这个

with list as (
select user_id, count(*) as user_count, array_agg(arrival_timestamp) as arrival_timestamp
from mytable
where date(arrival_timestamp) >= current_date-10
and date(arrival_timestamp) < current_date
group by user_id)
select user_id, unnest(arrival_timestamp)
from list
where user_count >= 4

【讨论】:

    【解决方案2】:

    从过去 10 天内每天访问您的商店 4 次或更多次的日常用户列表(内部查询)中选择出现 10 次的用户,即每天。

    select user_id
    from
    (
     select user_id 
     from the_table 
     where arrival_timestamp::date between current_date - 10 and current_date - 1
     group by user_id, arrival_timestamp::date
     having count(*) >= 4
    ) t
    group by user_id
    having count(*) = 10;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-04
      • 2012-08-22
      • 1970-01-01
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 2014-03-18
      • 1970-01-01
      相关资源
      最近更新 更多