我在我的数据库中有以下属性。
统计ID、设备名称、数值、时间戳。
对于给定的统计数据,我想要找到每个唯一设备的最近的两个时间戳和相应的值。
我正在尝试以下方法:
尝试1)
select statistic, devicename, value, timestamp
from X_STATSVALUE
where statistic=19
order by orgtime DESC limit 2;
这让我得到了前两个时间戳,但并非每个设备的。
试用2)
select statistic, devicename, value, timestamp
from X_STATSVALUE as x
where x.statistic=241
and (select count(*)
from X_STATSVALUE as y
where y.statistic=x.statistic
and y.device=x.device
and y.timestamp > x.timestamp) <=1;
但是这也不太有效...
基本上,我想要每个设备在给定统计数据中的最近两个时间戳和值。任何帮助都将不胜感激 :)