How can I select rows with most recent timestamp for each key value?

后端 未结 7 1914
栀梦
栀梦 2020-12-04 06:49

I have a table of sensor data. Each row has a sensor id, a timestamp, and other fields. I want to select a single row with latest timestamp for each sensor, including some o

7条回答
  •  无人及你
    2020-12-04 07:49

    WITH SensorTimes As (
       SELECT sensorID, MAX(timestamp) "LastReading"
       FROM sensorTable
       GROUP BY sensorID
    )
    SELECT s.sensorID,s.timestamp,s.sensorField1,s.sensorField2 
    FROM sensorTable s
    INNER JOIN SensorTimes t on s.sensorID = t.sensorID and s.timestamp = t.LastReading
    

提交回复
热议问题