I'm using MySQL, but I think this is a basic SQL question.
I don't know how else to ask but to give an example.
Say I have this data in my table:
id date_time foreign_key key value
1 2010-01-01 00:00:00 1 'temperature' 84
2 2010-01-01 00:00:01 1 'humidity' 34
3 2010-01-01 00:00:02 2 'temperature' 45
4 2010-01-01 00:00:03 2 'humidity' 23
5 2010-01-01 00:00:04 2 'dew_point' 78
6 2010-01-01 00:00:05 3 'temperature' 57
7 2010-01-01 00:00:06 3 'humidity' 41
8 2010-01-01 00:00:07 4 'temperature' 19
9 2010-01-01 00:00:08 4 'humidity' 35
10 2010-01-01 00:00:09 4 'dew_point' 24
11 2010-01-01 00:00:10 1 'temperature' 84
12 2010-01-01 00:00:11 1 'dew_point' 34
13 2010-01-01 00:00:12 2 'temperature' 45
14 2010-01-01 00:00:13 2 'humidity' 23
15 2010-01-01 00:00:14 3 'dew_point' 57
16 2010-01-01 00:00:15 3 'humidity' 41
17 2010-01-01 00:00:16 4 'temperature' 19
18 2010-01-01 00:00:17 4 'dew_point' 24
How do I get most recent of each key for a single foreign_key?
For Example, say I want the most recent for the foreign_key of 4, the result I want would be:
id date_time foreign_key key value
9 2010-01-01 00:00:08 4 'humidity' 35
17 2010-01-01 00:00:16 4 'temperature' 19
18 2010-01-01 00:00:17 4 'dew_point' 24
What is the SQL I would use to achieve this result?
As an aside, I realize that this is not the first way most would choose to store data like this, but I have my reasons. Namely that these values are reported separately from each other.