我有一个包含以下架构的表:
meshSetVersion integer
gatewayMAC text
deviceMAC text
sensorID integer
sensorValue text
timestamp time
首先,我找到最新的meshSetVersion:
(select max(meshSetVersion) from test where gatewayMAC='XXX') as lastV
然后我发现它的设备:
select distinct gatewayMAC, deviceMAC from test
where gatewayMAC='xxx' and meshSetVersion=lastV
然后我需要按时间戳
获取每个ID的最新sensorValue答案 0 :(得分:0)
一种方法使用distinct on
。我认为这是你想要的逻辑:
select distinct on (sensorId) t.*
from test t
where t.meshSetVersion = (select max(t2.meshSetVersion)
from test t2
where t2.gatewayMach = 'xxx'
)
order by sensorId, timestamp desc;