sql - Join with DateTime and Latest Value from another Table -
i have small database data weather parameters being recorded daily. microsoft sql server 2008 express database
my tables follows:
station (id, name, position) reading (station_id, timestamp, value) --station_id foreign key id in station table
i want join them , result below:
id | name | value | time --------+---------+------------------------- 0 | lake | 73 |2013/08/16 02:00 1 | pier | 72 |2013/08/16 02:00 2 | gate | 81 |2013/08/16 02:00
looking @ question join "latest" record t-sql, i've been able 1 row first table, , using join 2 tables, use latest value of right table, i've been able max time second table.
how can output want?
it can done subquery
select s.id, s.name, r.value, r.timestamp station s inner join reading r on s.id = r.station_id r.timestamp = ( select max(timestamp) reading reading.station_id = s.station_id )
Comments
Post a Comment