以15分钟为间隔对mysql进行分组查询

时间:2010-05-08 12:13:02

标签: mysql sql datetime group-by timestamp

我有一个监控系统,每n秒收集一次数据(n大约为10但是有所不同)。我想以15分钟的间隔汇总收集的数据。有没有办法将时间戳值合并为15分钟的块,以便分组工作?

10 个答案:

答案 0 :(得分:53)

SELECT   FLOOR(UNIX_TIMESTAMP(timestamp)/(15 * 60)) AS timekey
FROM     table
GROUP BY timekey;

答案 1 :(得分:12)

试试这个,将间隔15分钟的记录分组,你可以将15 * 60更改为你需要的时间间隔

SELECT sec_to_time(time_to_sec(datefield)- time_to_sec(datefield)%(15*60)) as intervals from tablename
group by intervals

答案 2 :(得分:4)

以下方法1)的改编:

select Round(date_format(date, "%i") / (15*60)) AS interval  
from table
group by interval 

以下方法3的改编:

SELECT Round(Convert(substring(date_column, 14, 2), UNSIGNED) / (15*60)) AS interval /* e.g. 2009-01-04 12:20:00 */
FROM table 
GROUP BY interval;

我找到的一些方法here

1)

select date_format(date, "%W") AS `Day of the week`, sum(cost)
from daily_cost
group by `Day of the week` 
order by date_format(date, "%w")

2)

select count(*) as 'count', 
  date_format(min(added_on), '%Y-%M-%d') as 'week commencing',
  date_format(added_on, '%Y%u') as 'week' 
from system 
where added_on >= '2007-05-16' 
group by week 
order by 3 desc;

3)

SELECT substring(postdate, 1,10) AS dd, COUNT(id) FROM MyTable GROUP BY dd;

(也在这里:http://www.bradino.com/mysql/dayparting-on-datetime-field-using-substring/

编辑:所有解决方案都会在包含大量记录的表格上表现不佳。

答案 3 :(得分:4)

我开始使用unutbu上面给出的答案,但没有得到我需要的东西,不得不添加一点。

Select Created, from_unixtime(FLOOR(UNIX_TIMESTAMP(Created)/(15*60))*(15*60)) GroupTime, COUNT(*) as Cnt FROM issue i GROUP BY GroupTime

此代码除以15分钟跨度内的900秒,然后将该值放大并将其乘以900,基本上向下舍入到最接近的15分钟增量。

答案 4 :(得分:3)

以下查询对行进行分组,并以15分钟的间隔创建时间戳。

Select concat( date(created_dt) , ' ', sec_to_time(time_to_sec(created_dt)- time_to_sec(created_dt)%(15*60) + (15*60)))as created_dt_new from table_name group by created_dt_new 


例如,时间戳 2016-11-09 13:16:29
2016-11-09 13:16:49
2016-11-09 13:17:06
2016-11-09 13:17:26
2016-11-09 13:18:24
2016-11-09 13:19:59
2016-11-09 13:21:17

分为2016-11-09 13:30:00

  1. sec_to_time(time_to_sec(created_dt)- time_to_sec(created_dt)%(15*60) + (15*60)))
    上限时间最接近15分钟。例如12:10 - > 12:15

  2. concat( date(created_dt) , ' ', sec_to_time(time_to_sec(created_dt)- time_to_sec(created_dt)%(15*60) + (15*60)))
    生成从时间戳字段中获取日期的时间戳。

答案 5 :(得分:2)

Unix时间戳:使用以下其中一种方法将它们设置为最接近15分钟:

timestamp div (15 * 60) * (15 * 60) -- div is integer division operator
timestamp - timestamp % (15 * 60)

日期时间:假设数据类型没有小数秒,请使用以下时间将其设置为最接近的15分钟:

date - INTERVAL EXTRACT(SECOND FROM date) SECOND - INTERVAL EXTRACT(MINUTE FROM date) % 15 MINUTE

DBFiddle

答案 6 :(得分:0)

这对我有用

mysql> **SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())- UNIX_TIMESTAMP(NOW())%(15*60));**
+---------------------------------------------------------------------+
| FROM_UNIXTIME(UNIX_TIMESTAMP(NOW())- UNIX_TIMESTAMP(NOW())%(15*60)) |
+---------------------------------------------------------------------+
| 2012-02-09 11:15:00                                                 |
+---------------------------------------------------------------------+
1 row in set (0.00 sec)

答案 7 :(得分:0)

为我工作

SELECT CONCAT (
        YEAR(transactionDate)
        ,'-'
        ,MONTH(transactionDate)
        ,'-'
        ,DAYOFMONTH(transactionDate)
        ,' '
        ,HOUR(transactionDate)
        ,':'
        ,((floor((MINUTE(transactionDate) / 15)) + 1) * 15) - 1
        ,':59'
        ) AS tmp1
    ,count(*)
FROM tablename
GROUP BY tmp1 limit 20;

答案 8 :(得分:0)

将“ 15”更改为所需的时间间隔。

select count(*),
CONCAT(HOUR(col_date),":",(MINUTE(create_date) div 15)*15) as date
from tablename
GROUP BY date
ORDER BY col_date ASC;

答案 9 :(得分:0)

我对GROUP BY不满意。

SELECT   datetime
FROM     table
WHERE MOD(MINUTE(TIME(datetime)),15) = 0 AND SECOND(TIME(datetime)) = 0;