【问题标题】:how to get result based on 10 minutes interval in mysql如何根据mysql中的10分钟间隔获取结果
【发布时间】:2018-12-25 09:41:36
【问题描述】:

如何在 10 分钟内找到十个搜索最多的不同目的地?

    user_id             time        action  destination place
    2017032000000000097 00:00:00    Click   Rimini  Regina Elena 57
    2017032000000000097 00:03:53    Click   Sant    Regina Elena 571
    2017032000000000097 00:01:16    Click   Regina  Regina Elena 572
    2017032000000000097 00:04:34    Click   Rimini  Regina Elena 57
    2017032000000000129 00:07:32    Click   Berlin  Müggelsee Berlin
    2017032000000000129 00:18:36    Click   GRC     SensCity Berlin Spandau
    2017032000000000129 00:16:12    Click   Berlin  Azimut Berlin City South

预期输出/类似输出

time            destination(top 10 during 10 minute interval) 
-------------   ---- 
00:00:00        NULL
00:10:00        Rimini,Sant,Regina
00:20:00        Berlin,Grc
00:30:00        NULL

我试过下面的代码,

select destination , count(user_id),time from click
where MINUTE(time)>= MINUTE(now())-10 and MINUTE(time)< minute(now()) and destination is not null
group by destination,MINUTE(time)>= MINUTE(now())-10 and MINUTE(time)< minute(now()) order by count(user_id) desc;

【问题讨论】:

  • 你到底想问什么?
  • @Jonathan 编辑了我的问题。
  • 你的时间格式到底是什么,你的输出没有多大意义?是 DD:HH:MM?
  • @JoakimDanielson 它是 HH:MM:SS,我已经编辑了我的输出
  • 您可能需要 GROUP_CONCAT 来获取示例中显示的连接列值。

标签: mysql sql


【解决方案1】:
select destination , count(id) from your_table
where MINUTE(time)>= MINUTE(now())-10 and MINUTE(time)< minute(now()) 
group by destination 
LIMIT 10

【讨论】:

    【解决方案2】:

    取每个时间的前三个字符,并在该子字符串上聚合。

    所以前五次变成00:0,接下来的三次变成00:1

    这样,十分钟间隔内的任何时间都会被截断为相同的内容。

    select substring(time,0,4) as truncTime, destination, count(*)
    from table
    group by truncTime
    

    给你

    truncTime  destination  count
    00:0       Rimini       4
    00:0       Berlin       1
    00:1       Berlin       2 
    

    【讨论】:

      【解决方案3】:

      我通过以下查询找到了解决方案。

      select a.time_column,group_concat(a.destination order by ct desc) from  (select case 
                  when time between '00:00:00' and '00:10:00' then '00:10:00'
                  when time between '00:10:01' and '00:20:00' then '00:20:00'
                  when time between '00:20:01' and '00:30:00' then '00:30:00'
              else '00:00:00' end as time_column 
              , destination
              , count(destination) ct
      from click
      group by time_column,destination
      order by time_column,count(destination) desc limit 10)a
      group by a.time_column;
      

      【讨论】:

        猜你喜欢
        • 2021-11-11
        • 1970-01-01
        • 2022-01-15
        • 2015-07-05
        • 2020-10-16
        • 1970-01-01
        • 2019-09-28
        • 2020-07-20
        • 1970-01-01
        相关资源
        最近更新 更多