join - SQL Server Matching Records over a Range of Valid Times -


i have trip table contains information of individual cars traveling in different parts of city (start time, end time, start location, end location, etc). table weather conditions (temp, precipitation, etc.) of said city measured semi-regularly (every 15 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 discrete make (more) continuous assume ids valid on range reading time midpoint. example:

  • weatherid 1 condition valid on 3:45 3:53:525
  • weatherid 2 condition valid on 3:53:525 3:16:195
  • etc

i want add foreign key trip table associates each trip weather present majority of trip. example tripid 2 takes 42 minutes complete occurs on 2 weather readings (weatherid 2 , 3). weatherid 3 assigned tripid 2 record because more of trip happens under weatherid 3 rather weatherid 2.

i understand bit complicated possible solve using sql? appreciated. thank.

if using recent version of sql server, weather condition covers largest portion of trip, try:

with tripsegs (wxid, starttime, endtime) (select w.weatherid,           coalesce(n.readingtime, w.readingtime) starttime,          coalesce(p.readingtime, w.readingtime) endtime     weather w     left join weather p on p.readingtime =         (select max(readingtime)             weather             readingtime < w.readingtime)     left join weather n on n.readingtime =         (select min(readingtime)             weather             readingtime > w.readingtime))  select t.tripid, w.wxid,      case when w.starttime < t.starttime t.starttime else w.starttime end starttime,     case when w.endtime > t.endtime t.endtime else w.endtime end endtime,     case when w.starttime < t.starttime t.starttime else w.starttime end -     case when w.endtime > t.endtime t.endtime else w.endtime end elapsedeffectivetime trips t join tripsegs w       on w.starttime < t.endtime   , w.endtime > t.starttime case when w.starttime < t.starttime t.starttime else w.starttime end -       case when w.endtime > t.endtime t.endtime else w.endtime end >      (select max(case when w.starttime < t.starttime t.starttime else w.starttime end -                case when w.endtime > t.endtime t..endtime else w.endtime end)      trips t     join tripsegs w on w.starttime < t.endtime          , w.endtime > t.starttime) 

Comments

Popular posts from this blog

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

html - How to style widget with post count different than without post count -

url rewriting - How to redirect a http POST with urlrewritefilter -