edit
I've realised that my question really has two parts:
- How do I group by time periods, and
- How do I select a particular row in a group?.
One of the answers to the second question uses Postgres' SELECT DISTINCT ON, which means I don't need a group at all. I've posted my solution below.
I have data which is normally queried to get the most recent value. However I need to be able to reproduce what results would have been received if I'd queried every minute, going back to some timestamp.
I don't really know where to start. I have very little experience with SQL.
CREATE TABLE history
(
detected timestamp with time zone NOT NULL,
stat integer NOT NULL
)
I select like:
SELECT
detected,
stat
FROM history
WHERE
detected > '2013-11-26 20:19:58+00'::timestamp
Obviously this gives me every result since the given timestamp. I want every stat
closest to minutes going back from now to the timestamp. By closest I mean 'less than'.
Sorry I haven't made a very good effort of getting anywhere near the answer. I'm so unfamiliar with SQL I don't know where to begin.
edit
This question, How to group time by hour or by 10 minutes, seems helpful:
SELECT timeslot, MAX(detected)
FROM
(
SELECT to_char(detected, 'YYYY-MM-DD hh24:MI') timeslot, detected
FROM
(
SELECT detected
FROM history
where
detected > '2013-11-28 13:09:58+00'::timestamp
) as foo
) as foo GROUP BY timeslot
This gives me the most recent detected
timestamp on one minute intervals.
How do I get the stat
? The MAX
is run on all the detected
grouped by minutes, but the stat
is inaccessible.
2nd edit
I have:
timeslot;max
"2013-11-28 14:04";"2013-11-28 14:04:05+00"
"2013-11-28 14:17";"2013-11-28 14:17:22+00"
"2013-11-28 14:16";"2013-11-28 14:16:40+00"
"2013-11-28 14:13";"2013-11-28 14:13:31+00"
"2013-11-28 14:10";"2013-11-28 14:10:02+00"
"2013-11-28 14:09";"2013-11-28 14:09:51+00"
I would like:
detected;stat
"2013-11-28 14:04:05+00";123
"2013-11-28 14:17:22+00";125
"2013-11-28 14:16:40+00";121
"2013-11-28 14:13:31+00";118
"2013-11-28 14:10:02+00";119
"2013-11-28 14:09:51+00";121
max
and detected
are the same