/** * @param \App\Repository\Entity\Station $station * @param \App\Repository\Entity\Component $component * @param \DateTime $start * @param \DateTime|null $to * * @return float|null */ public function getStationAverageForTimeAndComponent(Station $station, Component $component, \DateTime $start, \DateTime $to = null) { $query = $this->queryBuilder->from(self::$tableName); $query->where('station_id', $station->getId()); $query->where('component_id', $component->getId()); $query->where('measure_timestamp >= "' . $start->format("Y-m-d H:i:s") . '"'); if (isset($to)) { $query->where('measure_timestamp <= "' . $to->format("Y-m-d H:i:s") . '"'); } $query->select('AVG(value) AS average'); $result = $query->fetch('average'); return !empty($result) ? floatval($result) : null; }