dtgv52982 2018-02-03 17:47
浏览 59
已采纳

如何根据固定百分比计算不同的日期时间戳和过滤条目?

visitor ---  timestamp
abc     ---  2018-02-03 03:24:26
abc     ---  2018-02-03 02:20:20
abc     ---  2018-02-01 15:10:26
abc     ---  2018-01-31 18:24:26
abc     ---  2018-01-30 12:20:20
abc     ---  2018-01-26 10:10:26
def     ---  2018-02-03 03:24:26
def     ---  2018-01-30 12:20:20
def     ---  2018-01-26 10:10:26

I am trying to create a query that finds whether visitors abc, def are loyal.

My though is to have a fixed timeframe of 14 days. The question is how many times does a visitor signed in. In th case of abc had made visits in 5 unique days on this 14 days timeframe.

On the other hand, def made visits in 3 unique days.

What I want is to filter, let' say, which visitor was at least 30% loyal on this 14 days timeframe. In this example, abc is, but def is not.

The problems with the query:

  • it doesn't take into consideraton the distinct(date(timestamp) and it sums all the visits including multiple in a day for each visitor
  • I don't know how to filter them let's say 30% as mentioned above

SELECT visitor, Sum(DATE (TIMESTAMP) BETWEEN Date_sub(Curdate(), INTERVAL 14 day)
            AND Curdate()) AS last_twoWeeks
FROM behaviour
WHERE TIMESTAMP >= Curdate() - INTERVAL 14 day
GROUP BY visitor
ORDER BY last_twoweeks DESC
  • 写回答

1条回答 默认 最新

  • dongya6395 2018-02-03 17:49
    关注

    You are looking for count(distinct). To get only the "loyal" visitors, you can use a HAVING clause:

    SELECT visitor, 
           COUNT(DISTINCT Date(timestamp)) AS days_last_two_weeks
    FROM behaviour
    WHERE timestamp >= Curdate() - INTERVAL 14 day
    GROUP BY visitor
    HAVING days_last_two_weeks >= 0.3 * 14
    ORDER BY last_twoweeks DESC;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来