InfluxDB: selecting last() values from different tags

influx-dbqueryquery-performancetime-series-database

I have a bucket with the raw data of several sensors, things like temperature, air pressure and etc. The sensors don't send their data at the same time, which means that a given timestamp might have several data points from various sensors or just from just one.

Each reading is tagged with the ID of the sensor it comes from. I need to get the Last() reading from a given number of IDs. When I try this code:

select last(Temperature) from raw_measure where ID =~ /4372502|4399699|4406512|4407840/

instead of returning the last Temperature observation of each ID, it returns the most recent temperature timestamp of the whole group.

How can I get the last reading of each ID in a single query?

Best Answer

I found the solution. All you need to do is add an ORDER BY with the TAG field. So the end query becomes:

SELECT last(Temperature) 
FROM raw_measure 
where ID =~ /4372502|4399699|4406512|4407840/ 
ORDER BY ID