I have the following attributes in my DB.
statistic id, devicename, value, timestamp.
For a given statistic, I want to find the 2 most recent timestamps and corresopnding values for a every unique device.
I am trying stuff like
Trial 1)
select statistic, devicename, value, timestamp
from X_STATSVALUE
where statistic=19
order by orgtime DESC limit 2;
This gives me the top 2 timestamps, but not per device.
Trial 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;
But that's not working too well either..
Basically, I want the 2 most recent timestamps with values for each device, for a given statistic.. any help is really appreciated :)