I have my SQL data table for a sensor that is identified by a serial number and there are 4 output values for the sensor, that is 1,2,3,4.
I need to fetch the latest record for each sensor with the latest each output value(1,2,3,4)
Table
id serial output_value timestamp
-- ------ ------------ ---------
1 111 2 2020-12-1 01:00:06
2 111 2 2020-12-1 01:00:05
3 111 1 2020-12-1 01:00:04
4 111 3 2020-12-1 01:00:03
5 222 1 2020-12-1 01:00:02
6 222 3 2020-12-1 01:00:01
7 222 3 2020-12-1 01:00:00
I need to get the rows
id serial output_value timestamp
-- ------ ------------ ---------
1 111 2 2020-12-1 01:00:06
3 111 1 2020-12-1 01:00:04
4 111 3 2020-12-1 01:00:03
5 222 1 2020-12-1 01:00:02
6 222 3 2020-12-1 01:00:01
Command
SELECT DISTINCT ON (serial)
id,serial,output_value, timestamp
FROM sensorTable
ORDER BY serial, timestamp DESC;
But I have no idea how to add output_value
with it.
Mysql version
mysql 5.7.23, for Linux (x86_64) using EditLine wrapper