可能是这样的?
SQL> create table EVENTARCHIVE ( VEHID number, EVENTTIME date, EVENTDESCR varchar2(20));
Table created.
SQL> REM vehice 17 has no started moving record.
SQL> insert into EVENTARCHIVE values(17, to_date('24/12/2012 08:35:12', 'dd/mm/yyyy hh24:mi:ss'), 'stopped moving');
1 row created.
SQL> REM vehice 21 has multiple stop / starts
SQL> insert into EVENTARCHIVE values(21, to_date('24/12/2012 09:40:00', 'dd/mm/yyyy hh24:mi:ss'), 'stopped moving');
1 row created.
SQL> insert into EVENTARCHIVE values(21, to_date('24/12/2012 10:00:00', 'dd/mm/yyyy hh24:mi:ss'), 'started moving');
1 row created.
SQL> insert into EVENTARCHIVE values(21, to_date('24/12/2012 10:22:00', 'dd/mm/yyyy hh24:mi:ss'), 'stopped moving');
1 row created.
SQL> insert into EVENTARCHIVE values(21, to_date('24/12/2012 12:00:00', 'dd/mm/yyyy hh24:mi:ss'), 'started moving');
1 row created.
SQL> REM vehice 25 has multiple stop / starts records start with a started moving record.
SQL> insert into EVENTARCHIVE values(25, to_date('23/12/2012 16:35:00', 'dd/mm/yyyy hh24:mi:ss'), 'started moving');
1 row created.
SQL> insert into EVENTARCHIVE values(25, to_date('23/12/2012 22:59:00', 'dd/mm/yyyy hh24:mi:ss'), 'stopped moving');
1 row created.
SQL> insert into EVENTARCHIVE values(25, to_date('24/12/2012 07:00:00', 'dd/mm/yyyy hh24:mi:ss'), 'started moving');
1 row created.
SQL> REM vehice 33 has some duplicate stopped data (possible?)
SQL> insert into EVENTARCHIVE values(33, to_date('23/12/2012 16:35:00', 'dd/mm/yyyy hh24:mi:ss'), 'started moving');
1 row created.
SQL> insert into EVENTARCHIVE values(33, to_date('23/12/2012 22:59:00', 'dd/mm/yyyy hh24:mi:ss'), 'stopped moving');
1 row created.
SQL> insert into EVENTARCHIVE values(33, to_date('23/12/2012 23:02:00', 'dd/mm/yyyy hh24:mi:ss'), 'stopped moving');
1 row created.
SQL> insert into EVENTARCHIVE values(33, to_date('24/12/2012 07:00:00', 'dd/mm/yyyy hh24:mi:ss'), 'started moving');
1 row created.
SQL> commit;
Commit complete.
SQL> select vehid, eventtime stopped_moving, next_time started_moving
2 from (select vehid, eventtime, eventdescr,
3 case
4 when lead(eventdescr) over (partition by vehid order by eventtime) != eventdescr
5 then
6 lead(eventtime) over (partition by vehid order by eventtime)
7 end next_time
8 from EVENTARCHIVE)
9 where eventdescr = 'stopped moving'
10 and next_time is not null;
VEHID STOPPED_MOVING STARTED_MOVING
---------- -------------------- --------------------
21 24-dec-2012 09:40:00 24-dec-2012 10:00:00
21 24-dec-2012 10:22:00 24-dec-2012 12:00:00
25 23-dec-2012 22:59:00 24-dec-2012 07:00:00
33 23-dec-2012 23:02:00 24-dec-2012 07:00:00