简体   繁体   English

创建一个表,将观察结果与时间序列中以小时为单位的间隔进行分组

[英]Create a table that groups observations with gaps in hours in a time series

I receive some energy data from an external system named "mid" in a MySQL table. 我从MySQL表中名为“ mid”的外部系统接收一些能量数据。 Data is supposed to be transmitted every hour, but sometimes the system does not work and then skips an hours, eg. 数据应该每小时发送一次,但是有时系统无法工作,然后跳过一个小时,例如。 there is no data for 2014-8-13 10:00:00 for mid=2 in the example below. 在以下示例中,没有用于2014年8月13日10:00:00的数据,其中mid = 2。 I would like to create a table that identifies all the "mid" that have missing data - in this example that would be a table with mid=2 and datehour_in=2014-8-13 10:00:00. 我想创建一个表,该表标识所有缺少数据的“中间”-在此示例中,它将是一个中间= 2和datehour_in = 2014-8-13 10:00:00的表。

-- this is how my table looks like;
create table kwh_in(mid int, datehour_in datetime, kwh int);
-- these are some example values;
insert into kwh_in values(1,'2014-8-13 08:00:00', 52);
insert into kwh_in values(1,'2014-8-13 09:00:00', 51);
insert into kwh_in values(1,'2014-8-13 10:00:00', 47);
insert into kwh_in values(2,'2014-8-13 09:00:00', 28);
insert into kwh_in values(2,'2014-8-13 08:00:00', 31);
insert into kwh_in values(2,'2014-8-13 11:00:00', 32);

Had to rush and do this but have a look at this SQL Fiddle 不得不着急去做,但是看看这个SQL Fiddle

Essentially, create a reference table with all the hours of a day (any date). 本质上,创建一天中所有时间(任何日期)的参考表。 Right join onto it and then pull rows where datehour_in has a null value. 右键连接到它,然后拉出datehour_in具有空值的行。 Then pull the time that the reference table has but your source doesn't. 然后拉出引用表所具有的时间,而您的源所没有的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM