Here is my data structure :
name value date_received
foo 100 2013-09-19 10:00:00
bar 200 2013-09-19 10:00:00
foo 100 2013-09-19 10:05:00 //no change
bar 200 2013-09-19 10:05:00 //no change
foo 110 2013-09-19 10:08:00 // foo changed
bar 200 2013-09-19 10:08:00 // no change
......
Question:
I want a query (mysql) which can do something like:
select date_received where anyOf(foo, bar) changed from the previous
specified value in the past N hours.
There could be other names in the table but we are only interested in foo and bar.
Any pointers. To me it looks like we'll need a self join - but don't know how.
EDIT: looks like the below query is just a good starting point.
select date_received from (SELECT DISTINCT name, value from data) a
INNER JOIN (select DISTINCT name, value, date_received from data)b
on (a.name=b.name and a.value=b.value)
update Looks like below query works - easier than I thought it would be.
SELECT DISTINCT a.tr FROM (
SELECT name, value, MAX(date_received) dr from data
where date_received > now() - INTERVAL 2 hour
GROUP BY name, value order by dr desc)a;