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

后端 未结 7 1873
栀梦
栀梦 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
    
    0 讨论(0)
提交回复
热议问题