Hi all,
I’m trying to build a query that is a little out of my leak. The thread on http://www.sitepoint.com/forums/showthread.php?594274-How-do-I-return-a-group-count-for-every-15-minutes-of-the-hour-on-my-Table/page2 is almost the same what i want to achieve to. But (ofcourse) a little different:)
As i’m trying to build my own ‘smart home’ as a hobby project. I’ve put several sensors across my house. Temprature and humidity mostly for the moment. Every minute I poll those sensors and put the data in a MySQL database.
I use a very simple tabel:
CREATE DATABASE IF NOT EXISTS `Arduino`;
CREATE TABLE `sensorid18` (
`DateTime` datetime DEFAULT NULL,
`SensorValue` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*Data for the table `sensorid18` */
insert into `sensorid18` values ('2012-01-18 02:00:00',19);
insert into `sensorid18` values ('2012-01-18 02:15:00',18);
insert into `sensorid18` values ('2012-01-18 02:30:00',18);
insert into `sensorid18` values ('2012-01-18 02:45:00',17);
insert into `sensorid18` values ('2012-01-18 03:00:00',17);
insert into `sensorid18` values ('2012-01-18 03:15:00',17);
insert into `sensorid18` values ('2012-01-18 17:30:00',14);
insert into `sensorid18` values ('2012-01-18 17:45:00',14);
Note: De example data above is actually every minute. But i just post enough test data to support my question. And not all the data in my table.
From the thread I mentioned above, r937 explains very well how the build the query. Espacially the second question from olivier89. But I can’t get my head arround it to give me the results I want.
So far I took r937’s example from the excellent post above and altered it a little bit into this:
SELECT
FROM_UNIXTIME(FLOOR(UNIX_TIMESTAMP(sensorid18.DateTime)/900)*900) AS timeslice,
sensorid18.SensorValue AS SensorValue
FROM sensorid18
WHERE sensorid18.DateTime BETWEEN '20120118020000000' AND '20120118180000000'
GROUP BY timeslice
Order by sensorid18.DateTime ASC;
This gives me almost the results that i want, but not really. As you can see there is a gap between ‘2012-01-18 03:15:00’ and 2012-01-18 17:30:00’. So the result that i’m getting is:
timeslice,SensorValue
2012-01-18 02:00:00,19
2012-01-18 02:15:00,18
2012-01-18 02:30:00,18
2012-01-18 02:45:00,17
2012-01-18 03:00:00,17
2012-01-18 03:15:00,17
2012-01-18 17:30:00,14
2012-01-18 17:45:00,14
And what i need is records that will fill in the time gaps in between ‘2012-01-18 03:15:00,17’ and ‘2012-01-18 17:30:00,14’.
Like this:
timeslice,SensorValue
2012-01-18 02:00:00,19
2012-01-18 02:15:00,18
2012-01-18 02:30:00,18
2012-01-18 02:45:00,17
2012-01-18 03:00:00,17
2012-01-18 03:15:00,17
2012-01-18 03:30:00,0
2012-01-18 03:45:00,0
2012-01-18 04:00:00,0
2012-01-18 04:15:00,0
2012-01-18 04:30:00,0
2012-01-18 04:45:00,0
...
2012-01-18 17:30:00,14
2012-01-18 17:45:00,14
I know that i must use the INNER JOIN statement. But i can’t get my head around it. I know there are guy’s here that know how to do it I’ve beeing reading many threads on this forum
So, who is giving me a nutch in the right direction ?
Thank you in advange,
Greetings from Belgiüm.
Sven.