I need to make a graph from a log. The log entries are not in regular intervals.
I would like to select rows between dates along with what the values were immediately before the start date (that is, from whenever the immediatly preceeding log was entered).
So, let's say:
table Foo
has id
and value
columns,
table Bar
has id
, foo_id
, and value
columns, and
table BarLog
has id
, foo_id
, bar_id
, bar_value
and timestamp
.
So there can be many Bars for one Foo.
I need all rows from BarLog for all Bars given some foo_id
between, say, 07/01/2012
and 07/31/2012
and the value (row) for each Bar as it was on 07/01/2012
.
Hope that made sense, if not, I'll try to clarify.
EDIT (above left for context):
Let's simplify this down another step. If I have a table with two foreign keys, fk_a
and fk_b
, and a timestamp
, how can I get the most recent rows with a given fk_a
and a distict fk_b
.
As suggested, here's an example.
+----+------+------+-------------+
| id | fk_a | fk_b | timestamp |
+----+------+------+-------------+
| 1 | 1 | 1 | 01-JUL-2012 |
| 2 | 2 | 2 | 02-JUL-2012 |
| 3 | 1 | 1 | 04-JUL-2012 |
| 4 | 2 | 2 | 05-JUL-2012 |
| 5 | 1 | 3 | 07-JUL-2012 |
+----+------+------+-------------+
Given a fk_a
of 1, I would want rows 3 and 5. So looking only at rows 1, 3, and 5 (those with fk_a
of 1), get the most recent of each fk_b
(where row 3 is more recent than row 1 for fk_b=1
).
Thanks again.