I have the following a doubt regarding the code that I implemented. Although it is functional, I am not convinced by the way in which I managed to solve the problem.
What it does is calculate the average of the recorded data at the exact times (ex: 08:00:00, 09:00:00) taking EL records of the previous 5 minutes, and EL of the 5 subsequent minutes. Ex: (05:55:00 + 06:00:00 + 06:05:00) / 3 and it gives me something like this
| temp | fecha
|______|____________________
| 21.1 | 2018-12-06 06:00:00
Because I need the last 12 records with the previous calculation mentioned I do it by making 3 queries for the minutes specific and the last hours recorded ordering by date and time the query. After obtaining the 3 individual queries with limit of 12 and the first one with offset, in php process to take the records obtained, do the calculation and deliver an array similar to this:
| temp | fecha
|______|____________________
| 21.1 | 2018-12-06 01:00:00
| 21.2 | 2018-12-06 02:00:00
| 21.3 | 2018-12-06 03:00:00
| 21.4 | 2018-12-06 04:00:00
| 21.5 | 2018-12-06 05:00:00
| 21.6 | 2018-12-06 06:00:00
| 21.7 | 2018-12-06 07:00:00
Although, this solution allows me to deliver the requested data, I would like to know a way to obtain everything in a single query, including calculating the average if possible.
This is the code that I have in the model.
(unfortunately, when the database was modeled, the date and time were separated: c)
as additional data after this goes to a controller that delivers the response as json to load it into a hightchart graph:)
$query1 = "SELECT temp,hora,fecha FROM estacion WHERE hora like '%:55:00' ORDER BY fecha DESC, hora DESC LIMIT 12 OFFSET 1";
$query2 = "SELECT temp,hora,fecha FROM estacion WHERE hora like '%:00:00' ORDER BY fecha DESC, hora DESC LIMIT 12";
$query3 = "SELECT temp,hora,fecha FROM estacion WHERE hora like '%:05:00' ORDER BY fecha DESC, hora DESC LIMIT 12";
$consulta1 = $this->db->query($query1);
$consulta1 = $consulta1->result_array();
$consulta2 = $this->db->query($query2);
$consulta2 = $consulta2 ->result_array() ;
$consulta3 = $this->db->query($query3);
$consulta3 = $consulta3 ->result_array() ;
foreach($consulta1 as $row => $index)
{
date_default_timezone_set('UTC');
//calcula temperatura
$informacion = (($index[$dato])+($consulta2[$row][$dato])+($consulta3[$row][$dato]))/3;
$informacion = number_format($informacion, 2, '.', '');
//arregla fecha y hora
$fecha = $consulta2[$row]['fecha'] . " " . $consulta2[$row]['hora'];
$fecha = strtotime($fecha) * 1000;
$datos[$row][0] = $fecha;
$datos[$row][1] = $informacion;
}
//print_r( $datos );
//$consulta->result_array();
return $datos;
I leave the create table in case it is necessary
CREATE TABLE 'estacion' ( 'fecha' date NOT NULL, 'hora' time NOT NULL, 'temp' float(3,1) DEFAULT NULL, 'humedad' int(3) DEFAULT NULL, 'pRocio' float(3,1) DEFAULT NULL, 'vPromedio' float(3,1) DEFAULT NULL, 'vRafaga' float(3,1) DEFAULT NULL, 'direcViento' int(3) DEFAULT NULL, 'precActual' float(5,1) DEFAULT NULL, 'precHoy' float(5,1) DEFAULT NULL, 'presion' float(7,2) DEFAULT NULL, 'precTotal' float(7,2) DEFAULT NULL, 'tempInterior' float(3,1) DEFAULT NULL, PRIMARY KEY ('fecha','hora') ) ENGINE=InnoDB DEFAULT CHARSET=latin1
example data
hora | temp | fecha
________|______|____________
03:55:00 | 21.1 | 2018-12-06
04:00:00 | 20.2 | 2018-12-06
04:05:00 | 24.2 | 2018-12-06
...
04:55:00 | 10.2 | 2018-12-06
05:00:00 | 12.2 | 2018-12-06
05:05:00 | 16.2 | 2018-12-06
...
05:55:00 | 22.2 | 2018-12-06
06:00:00 | 24.2 | 2018-12-06
06:05:00 | 26.2 | 2018-12-06