简体   繁体   中英

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. 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. 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.

-- 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

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. Then pull the time that the reference table has but your source doesn't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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