使用SQL以时间序列计算时间间隔

时间:2011-02-22 22:13:49

标签: mysql sql datetime time

我有一个像这样的MySQL表

CREATE TABLE IF NOT EXISTS `vals` (
  `DT` datetime NOT NULL,
  `value` INT(11) NOT NULL,
  PRIMARY KEY (`DT`)
);

DT是具有时间的唯一日期

数据样本:

INSERT INTO `vals` (`DT`,`value`) VALUES
('2011-02-05 06:05:00', 300),
('2011-02-05 11:05:00', 250),
('2011-02-05 14:35:00', 145),
('2011-02-05 16:45:00', 100),
('2011-02-05 18:50:00', 125),
('2011-02-05 19:25:00', 100),
('2011-02-05 21:10:00', 125),
('2011-02-06 00:30:00', 150);

我需要得到这样的东西:

start|end|value
NULL,'2011-02-05 06:05:00',300
'2011-02-05 06:05:00','2011-02-05 11:05:00',250
'2011-02-05 11:05:00','2011-02-05 14:35:00',145
'2011-02-05 14:35:00','2011-02-05 16:45:00',100
'2011-02-05 16:45:00','2011-02-05 18:50:00',125
'2011-02-05 18:50:00','2011-02-05 19:25:00',100
'2011-02-05 19:25:00','2011-02-05 21:10:00',125
'2011-02-05 21:10:00','2011-02-06 00:30:00',150
'2011-02-06 00:30:00',NULL,NULL

我尝试了以下查询:

SELECT T1.DT AS `start`,T2.DT AS `stop`, T2.value AS value FROM (
  SELECT DT FROM vals
) T1
LEFT JOIN (
  SELECT DT,value FROM  vals
) T2
ON T2.DT > T1.DT ORDER BY T1.DT ASC

但它返回到结果中的许多行(29而不是9),我觉得没有办法用SQL限制它。在MySQL中可能吗?

3 个答案:

答案 0 :(得分:4)

使用子查询

SELECT
  (
     select max(T1.DT)
     from vals T1
     where T1.DT < T2.DT
  ) AS `start`,
  T2.DT AS `stop`,
  T2.value AS value
FROM vals T2
ORDER BY T2.DT ASC

您还可以使用采用变量的MySQL特定解决方案

SELECT CAST( @dt AS DATETIME ) AS `start` , @dt := DT AS `stop` , `value` 
FROM (SELECT @dt := NULL) dt, vals
ORDER BY dt ASC

但你需要准确地做到这一点

  • ORDER by必须存在,否则变量无法正常滚动
  • 变量需要在查询中使用子查询进行设置,否则如果连续运行两次,第二次不会以NULL开头

答案 1 :(得分:0)

您可以使用服务器端变量来模拟它:

select @myvar as start, end, value, @myvar := end as next_rows_start
from vals

变量按顺序从左右开始解释,因此对@myvar(start和next_rows_start)的两个引用将输出两个不同的值。

请记住在查询之前和/或之后将@myvar重置为null,否则第二行和后续运行将有错误的第一行:

select @myvar := null

答案 2 :(得分:0)

如果表具有与DT中的时间(相同顺序)相对应的运行ID列,则这将更容易。如果您不想更改表,可以使用temp:

drop table if exists temp;

CREATE TABLE temp (
  `id` INT(11) AUTO_INCREMENT,
  `DT` datetime NOT NULL,
  `value` INT(11) NOT NULL,
  PRIMARY KEY (`id`)
);

insert into temp (DT,value) select * from vals order by DT asc;

select t1.DT as `start`, t2.DT as `end`, t2.value 
from  temp t2
left join temp t1 ON t2.id = t1.id + 1;