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 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效