Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/80.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在mysql中以10分钟为间隔获得结果_Mysql_Sql - Fatal编程技术网

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

如何在mysql中以10分钟为间隔获得结果,mysql,sql,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

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

每次取前三个字符,并在该子字符串上进行聚合

前五次变成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 

我通过下面的查询找到了解决方案

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;

“你的问题到底是什么?”乔纳森编辑了我的问题。你的时间格式到底是什么,你的输出没有多大意义?它是DD:HH:MM吗?@JoakimDanielson它是HH:MM:SS,我已经编辑了我的输出您可能需要GROUP_CONCAT来获取示例中显示的连接列值。
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;