doujianchao7446 2015-04-07 18:32
浏览 96
已采纳

在MySql中按日期查找新客户和重复客户

I have a visit table with user_id and visited_at columns. I would like to find how many are new and repeat customer on each day in this month.

user_id      visited_at                

--------------------------------------------------------------------------

1750        2015-01-28 12:38:59
1920        2015-01-19 17:20:20
1009        2015-01-17 18:20:12
1920        2015-01-17 15:10:10
1080        2015-01-13 20:18:41
1920        2014-04-04 10:31:15
1750        2013-10-04 10:39:20

In January 2015, user 1750 and 1920 visited the same place so total repeated customers are 2. In April 2015, user 1750, 1920 and 1080 visited the same place so total repeated customers are 3. The output should be something like this

October 2013

Month               New        Repeat
----------------------------------------------
2013-10-04          1          0

April 2014

Month               New        Repeat
----------------------------------------------
2014-04-04          1          0

January 2015

Month               New        Repeat
----------------------------------------------
2015-01-13          1          0
2015-01-17          1          1
2015-01-19          0          1
2015-01-28          0          1
  • 写回答

1条回答 默认 最新

  • dou4381 2015-04-07 21:13
    关注

    Chopra . . . I actually did misunderstand the question you posed in the comment. This is very similar to the structure by month, just the aggregation is by date instead:

    select date(v.visited_at),
           count(case when v.visited_at = vv.minva then user_id end) as num_new_users,
           (count(distinct user_id) - count(case when v.visited_at = vv.minva then user_id end)
           ) as num_repeat_users
    from visits v join
         (select user_id, min(visited_at) as minva
          from visits t
          group by user_id
         ) vv
         on v.user_id = vv.user_id
    group by date(v.visited_at)
    order by date(v.visited_at);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题