I have a situation where I want to connect two tables that do not have a key to link them within Oracle 8i. I have Table A with some information and a datetime and Table B with some information and a datetime. What I would like is everything from Table A with the corresponding data from Table B for the time in Table A I’ll mock it up below:
Table A | Table B
John 31/07/2012 10:50:32 | Travel to Library 31/07/2012 10:48:23
John 31/07/2012 10:55:49 | Arrive at Library 31/07/2012 11:02:34
John 31/07/2012 11:00:15 | Reading Book 31/07/2012 11:07:11
John 31/07/2012 11:05:23 |
John 31/07/2012 11:10:12 |
What I want to create is this:
John 31/07/2012 10:50:32 Travel to Library
John 31/07/2012 10:55:49 Travel to Library
John 31/07/2012 11:00:15 Travel to Library
John 31/07/2012 11:05:23 Arrive at Library
John 31/07/2012 11:10:12 Reading Book
As might be suspected this is in relation to a table that contains GPS data that I receive every 5 minutes and another table completely unrelated that lists activities. My closest attempt so far has been to write a query in two parts the first part is looking at the activity and returning an activity date and a next activity date then connecting the two tables based on the name of the person involved and returning the GPS data when it is between the two activity dates, I’ve also done it the opposite way around with GPS date and Next GPS date and returning the activity if it is between those. Neither way is returning all of either set of data.
I feel that my major malfunction lies in my use of the leap/lag functions to get a before or after datetime. Perhaps I’m just overlooking the obvious answer.
I could really use some help to point me in the right direction with this.
Try this:
Note: I assumed that the GPS data also comes with a name in the table.