如何在mysql中以10分钟为间隔获取结果

vktxenjb  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(555)

如何在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;
toiithl6

toiithl61#

取每一次的前三个字符,并在该子串上进行聚合。
所以前五次变成 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
yrwegjxp

yrwegjxp2#

我通过下面的查询找到了解决办法。

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;
r7s23pms

r7s23pms3#

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

相关问题