SQL Server Matching Records over a Range of Valid Times
I have a trip table which contains information of individual cars
traveling in different parts of a city (start time, end time, start
location, end location, etc). I also a table with the weather conditions
(temp, precipitation, etc.) of said city measured semi-regularly (every 15
to 30 minutes).
Trip Table
| TripID | StartTime | EndTime | ....
| 1 | 2012-01-10 03:50:00.163 | 2012-01-15 04:15:40.163 |
| 2 | 2012-01-10 03:59:00.113 | 2012-01-15 04:44:25.025 |
| 3 | 2012-01-10 04:10:00.127 | 2012-01-15 04:35:36.064 |
Weather Table
| WeatherID | ReadingTime | ....
| 1 | 2012-01-10 03:45:00 |
| 2 | 2012-01-10 04:02:05 |
| 3 | 2012-01-10 04:30:34 |
| 4 | 2012-01-10 04:45:23 |
These weather readings are discrete so to make it (more) continuous I
assume the ids valid over a range with the reading time as the midpoint.
For example:
WeatherID 1 condition is valid over 3:45 to 3:53:525
WeatherID 2 condition is valid over 3:53:525 to 3:16:195
etc
I want to add a foreign key to the trip table which associates each trip
to the weather what was present for the MAJORITY of the trip. For example
TripID 2 takes 42 minutes to complete which occurs over two weather
readings (WeatherID 2 and 3). So WeatherID 3 will be assigned to the
tripID 2 record because more of the trip happens under weatherID 3 rather
than weatherID 2.
I understand this is a bit complicated but is it possible to solve using
SQL? Any help is appreciated. Thank.
No comments:
Post a Comment