Advanced MySQL query to get range of latest records with same id and different subids -
i have table called sensorvalue this:
station_id sensor_id timestamp value ---------------------------------------------------------- 1 1 2013-09-04 12:00:00 12.2 1 2 2013-09-04 12:00:00 13.1 1 3 2013-09-04 12:00:00 13.2 1 1 2013-09-04 12:05:00 12.1 1 2 2013-09-04 12:05:00 14.1 1 3 2013-09-04 12:05:00 13.2 1 1 2013-09-04 12:10:00 12.5 1 2 2013-09-04 12:10:00 13.3 1 3 2013-09-04 12:10:00 14.1 i need mysql query gets latest values of 3 sensors of station station_id 1. wonder if possible 1 mysql statement.
i've managed data out of table, not in efficient way. first select different sensors station 1 , afterthat call mysql select statement foreach sensor. of stations have on 10 sensors, approach need @ least 11 mysql statements data out of database.
i include in 1 mysql statement. possible? , how?
retrieve latest reading sensors on stations.
select s.* (select station_id, sensor_id, max(timestamp) max_timestamp sensorvalue group station_id, sensor_id ) m join sensorvalue s on s.station_id = m.station_id , s.sensor_id = m.sensor_id , s.timestamp = m.max_timestamp to limit specific station.
select s.* (select station_id, sensor_id, max(timestamp) max_timestamp sensorvalue station_id = 1 group station_id, sensor_id ) m join sensorvalue s on s.station_id = m.station_id , s.sensor_id = m.sensor_id , s.timestamp = m.max_timestamp
Comments
Post a Comment