简体   繁体   English

获取设备集中的最新传感器值

[英]Getting latest sensor value in set of devices

I have a table with following schema: 我有一个具有以下架构的表:

meshSetVersion integer
gatewayMAC text
deviceMAC text
sensorID integer
sensorValue text
timestamp time

First I find the latest meshSetVersion with: 首先,我找到最新的meshSetVersion:

(select max(meshSetVersion) from test where gatewayMAC='XXX') as lastV

Then I find it's devices: 然后我发现它是设备:

select distinct gatewayMAC, deviceMAC from test
where gatewayMAC='xxx' and meshSetVersion=lastV

Then I need to get latest sensorValue of each ID by timestamp 然后我需要通过时间戳获取每个ID的最新sensorValue

One method uses distinct on . 一种方法distinct on使用distinct on I think this is the logic that you want: 我认为这是您想要的逻辑:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM