tags:

views:

24

answers:

1

Okay so right now I'm working on a program that has a whole bunch of event objects. Each event object has a start time and end time (both full timestamps). A lot of the events are contiguous (one event's endTime is the same as the next event's startTime) showing that the two events happened in succession. If I were to store all my events in a SQLite or MySQL table, would it be possible to query for each set of contiguous events?

Say my table looks like this.

id   title          start_time           end_time
----------------------------------------------------------
1    Happy Time     4/3/2010 10:00:00    4/3/2010 13:00:00
2    Happier Time   4/3/2010 13:00:00    4/3/2010 18:00:00
3    Good Time      4/4/2010 18:00:00    4/4/2010 19:00:00
4    Gooder Time    4/4/2010 19:00:00    4/4/2010 22:00:00
5    Sad Time       4/6/2010 16:00:00    4/6/2010 20:00:00

I want to be able to query this table and see that id 1 and 2 are contiguous, id 3 and 4 are contiguous and id 5 is... contiguous with itself.

The reason I don't know where to start is because whenever I have worked with SQL queries, results were returned as individual rows. One row in the result is never part of a subgrouping.

If you have any questions feel free to comment. Thanks!

+2  A: 
SELECT a.id, a.title, a.start_time, a.end_time, b.id, b.title, b.start_time, b.end_time
FROM TABLE1 a, TABLE1 b
WHERE a.start_time = b.end_time

EDIT: modified based on the additional information:

SELECT a.id, a.title, a.start_time, a.end_time, b.id, b.title, b.start_time, b.end_time
FROM TABLE1 a left join TABLE1 b ON a.start_time = b.end_time
Russ
The only problem with this query is that it leaves out events that do not have any other events contiguous with it. I would like to also select those. What's the best way to do that do you think?
Joel Verhagen
Added second SQL statement based on the new information.
Russ