SQL开发知识:MySql数据库时间序列间隔查询方式
mysql时间序列间隔查询
在时间序列处理中,采集到的数据保存在数据表中,采集的频率可能是固定间隔(10秒,1小时或者1天),但往往是不固定的。
处理的时候要固定一个时间间隔进行查询,比如10秒一次的采样,处理的时候变成每小时一次。
可以用以下方法:
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_RealData`(IN `timeBegin` DATETIME, IN `timeEnd` DATETIME, IN `timeInterval` INT)
MODIFIES SQL DATA
SQL SECURITY INVOKER
COMMENT ‘/*存储过程:按起始、终止时间,以及时间间隔,抽取数据*/’
BEGIN
SET @time := ‘1000-01-01 00:00:00’;
SET @interval := timeInterval;
SELECT * FROM realdata_table
WHERE `realdata_table`.`received_time` >= timeBegin AND `realdata_table`.`received_time` <= timeEnd
AND TIMESTAMPDIFF( SECOND, @time, `realdata_table`.`received_time` ) >= @interval
AND @time := `realdata_table`.`received_time`;
END$$
DELIMITER ;
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_RealData`(IN `timeBegin` DATETIME, IN `timeEnd` DATETIME, IN `timeInterval` INT)
MODIFIES SQL DATA
SQL SECURITY INVOKER
COMMENT ‘/*存储过程:按起始、终止时间,以及时间间隔,抽取数据*/’
BEGIN
SET @time := ‘1000-01-01 00:00:00’;
SET @interval := timeInterval;
SELECT * FROM realdata_table
WHERE `realdata_table`.`received_time` >= timeBegin AND `realdata_table`.`received_time` <= timeEnd
AND TIMESTAMPDIFF( SECOND, @time, `realdata_table`.`received_time` ) >= @interval
AND @time := `realdata_table`.`received_time`;
END$$
DELIMITER ;
调用的时候:
SET @p0=’2016-03-29 00:00:00′;
SET @p1=’2016-03-31 00:00:00′;
SET @p2=’3600′;
CALL `proc_RealData`(@p0, @p1, @p2);
SET @p1=’2016-03-31 00:00:00′;
SET @p2=’3600′;
CALL `proc_RealData`(@p0, @p1, @p2);
mysql查询指定时间间隔数据
CREATE DEFINER=`root`@`localhost` PROCEDURE `proc_RealData`(IN `timeBegin` DATETIME, IN `timeEnd` DATETIME, IN `timeInterval` INT)
MODIFIES SQL DATA
SQL SECURITY INVOKER
COMMENT ‘/*存储过程:按起始、终止时间,以及时间间隔,抽取数据*/’
BEGIN
SET @time := ‘1000-01-01 00:00:00’;
SET @interval := timeInterval;
SELECT * FROM em_air_humidity
WHERE `em_air_humidity`.`DateTime` >= timeBegin AND `em_air_humidity`.`DateTime` <= timeEnd
AND TIMESTAMPDIFF( SECOND, @time, `em_air_humidity`.`DateTime` ) >= @interval
AND @time := `em_air_humidity`.`DateTime`;
END
MODIFIES SQL DATA
SQL SECURITY INVOKER
COMMENT ‘/*存储过程:按起始、终止时间,以及时间间隔,抽取数据*/’
BEGIN
SET @time := ‘1000-01-01 00:00:00’;
SET @interval := timeInterval;
SELECT * FROM em_air_humidity
WHERE `em_air_humidity`.`DateTime` >= timeBegin AND `em_air_humidity`.`DateTime` <= timeEnd
AND TIMESTAMPDIFF( SECOND, @time, `em_air_humidity`.`DateTime` ) >= @interval
AND @time := `em_air_humidity`.`DateTime`;
END
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。