What I thought would be a simple thing to solve has now bugged for quite some time. Now I need help from you guys.
In Informix I have a table "temperatures" like this:
locId dtg temp 100 2009-02-25 10:00 15 200 2009-02-25 10:00 20 300 2009-02-25 10:00 24 100 2009-02-25 09:45 13 300 2009-02-25 09:45 16 200 2009-02-25 09:45 18 400 2009-02-25 09:45 12 100 2009-02-25 09:30 11 300 2009-02-25 09:30 14 200 2009-02-25 09:30 15 400 2009-02-25 09:30 10
I am trying to get the latest temp for each locId that has updated values within the last 20 min.
So the result I want from the table above would be (say I run the query at 2009-02-25 10:10):
locId dtg temp 100 2009-02-25 10:00 15 200 2009-02-25 10:00 20 300 2009-02-25 10:00 24
One more thing that complicates things is that I would like to be able to supply a list on locId that should be selected. I mean use something like "...locId IN (100,200,400)..."
I have tried to use a join on a sub query (as suggested in SQL Query to get latest price ) but I can´t get it to work. Not even without the extra "update within the last 20 min".
select t.* from temperatures as t JOIN (select locId, max(dtg) from temperatures where locId IN (100,200,400) group by locId) as l on l.locId=t.locId and l.dtg=t.dtg where locId in (100,200,400)
This query gives me SQL error but I cant find error. Is there an error I can´t find or is this way to do it not possible in Informix.
Or is there some other way to go? All help appreciated.