Suppose I've got a table containing train departure and arrival times,
like so:
SELECT id,departure,arrival,arrival-departure AS duration FROM timetable;
id | departure | arrival | duration
----+------------------------+------------------------+----------
1 | 2002-12-02 14:00:00+00 | 2002-12-02 15:00:00+00 | 01:00
7 | 2002-12-02 17:00:00+00 | 2002-12-03 14:00:00+00 | 21:00
2 | 2002-12-03 17:00:00+00 | 2002-12-03 18:00:00+00 | 01:00
6 | 2002-12-03 21:00:00+00 | 2002-12-04 04:00:00+00 | 07:00
4 | 2002-12-04 10:00:00+00 | 2002-12-04 18:00:00+00 | 08:00
5 | 2002-12-05 08:00:00+00 | 2002-12-05 10:00:00+00 | 02:00
Can anyone advise as to the best way to express the following question
in SQL: "What's the earliest arrival time where there isn't a
subsequent departure time within (e.g.) 7 hours?". The table will
contain many rows.
I feel foolish - it _looks_ simple - but I've been banging my head
against the wall trying to implement this as either a JOIN or a
subselect, and it's starting to hurt.
Malcolm.