如何基于mysql中的10分钟间隔获取结果

时间:2018-07-17 15:48:38

标签: mysql sql

如何以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;

3 个答案:

答案 0 :(得分:0)

每次取前三个字符,并汇总到该子字符串上。

因此前五次变为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 

答案 1 :(得分:0)

select destination , count(id) from your_table
where MINUTE(time)>= MINUTE(now())-10 and MINUTE(time)< minute(now()) 
group by destination 
LIMIT 10

答案 2 :(得分:0)

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

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;