I have two tables:
Events (ID, Name, Time, Recorder)
Video (ID, StartTime, EndTime, Recorder, Filename)
I wish to join the Event data to the video data, so that for every event I get the video filename. The recorder field is used to specify which recorder was operable at the event’s time, and assists with multiple recorders recording video at the same time.
If i wasn’t concerned about the events which have no video then this is fine (I can get the SQL), however in my case i wish to show the closest video filename and the seconds difference.
EDIT
Sample Data
Events
1, EV1, 2012-01-01 12:00, A
2, EV2, 2012-01-01 13:00, B
3, EV3, 2012-01-01 12:15, B
4, EV4, 2012-01-01 11:45, A
Video
1, 2012-01-01 12:00, 2012-01-01 12:30, A, 1.mpg
2, 2012-01-01 13:00, 2012-01-01 13:30, A, 2.mpg
3, 2012-01-01 12:00, 2012-01-01 12:30, B, 3.mpg
Result
(EventID, VideoID, Filename, IsBetween, SecondsDifference)
1, 1, 1.mpg, TRUE, 0
2, 3, 3.mpg, FALSE, 1800 //1800 seconds from the end of video 3
3, 3, 3.mpg, TRUE, 900
4, 1, 1.mpg, FALSE, 900 //900 seconds from the start of video 1
BONUS
I would be even nicer if the closest video did not take the recorder into account (but the first bounds (Start and End) check to take it into account) If this is too difficult then thats fine.
It’s a little clunky, but here’s what I came up with:
This returns events whose dates don’t fall between any of the time ranges of any video, but then returns the video that falls the closest to that event date.
The only thing right now is that there are some videos where the seconds difference is exactly the same. I don’t know if you want it to return 2 rows, but for now, I put in the GROUP BY to just select one.
Let me know how that works.