2014-05-05 54 views
0

我已經得到了有關選擇和組合時間 例如一個問題,我得到了下面的時間在數據庫:如何選擇和每5秒間隔分組日期?

2014-03-31 00:00:06 
2014-03-31 00:00:06 
2014-03-31 00:00:06 
2014-03-31 00:00:16 
2014-03-31 00:00:16 
2014-03-31 00:00:16 
2014-03-31 00:00:22 
2014-03-31 00:00:22 
2014-03-31 00:00:22 
2014-03-31 00:00:26 
2014-03-31 00:00:26 
2014-03-31 00:00:26 
2014-03-31 00:00:39 
2014-03-31 00:00:39 
2014-03-31 00:00:39 
2014-03-31 00:00:42 
2014-03-31 00:00:42 
2014-03-31 00:00:42 
2014-03-31 00:00:49 
2014-03-31 00:00:49 
2014-03-31 00:00:49 
2014-03-31 00:00:58 
2014-03-31 00:00:58 
2014-03-31 00:00:58 
2014-03-31 00:01:30 
2014-03-31 00:01:30 
2014-03-31 00:01:30 
2014-03-31 00:01:39 
2014-03-31 00:01:39 
2014-03-31 00:01:39 
2014-03-31 00:01:40 
2014-03-31 00:01:40 
2014-03-31 00:01:40 
2014-03-31 00:01:47 
2014-03-31 00:01:47 
2014-03-31 00:01:47 
2014-03-31 00:02:06 
2014-03-31 00:02:06 
2014-03-31 00:02:06 
2014-03-31 00:02:09 

而且我想他們的5 seconds間隔組,指望他們 EG。

2014-03-31 00:00:00 Count 0 
2014-03-31 00:00:05 Count 3 
2014-03-31 00:00:10 Count 0 
2014-03-31 00:00:15 Count 3 
2014-03-31 00:00:20 Count 3 
etc.... 

我試過使用date_format,似乎我不能按預期對它們進行分組。

謝謝所有的SOers。

回答

0
select concat(date_format(dt,'%Y-%m-%d H:%i:'),lpad(floor(second(dt)/5)*5,2,'0')), count(*) 
    from dates 
group by concat(date_format(dt,'%Y-%m-%d %H:%i:'),lpad(floor(second(dt)/5)*5,2,'0')); 
0

試試這個:

SELECT sec_to_time(time_to_sec(col1)- time_to_sec(col1)%(5)) AS timekey, count(*) 
FROM  mytable 
GROUP BY timekey; 

小提琴例如: SQL Fiddle