Connecting Tech Pros Worldwide Help | Site Map

Group consecutive records

Newbie
 
Join Date: Oct 2008
Location: Lebanon
Posts: 2
#1: Oct 29 '08
I know what I am asking for is pretty hard, but still maybe there is someone out there who is able to solve my problem!

I have a table that look like this
CREATE TABLE VEHICLETRIPS
( VID NUMBER(10) NOT NULL,
VECHIELID VARCHAR2(10) NOT NULL,
DEVICEDATE DATE NOT NULL,
TRIPID NUMBER(10) NOT NULL,
COMMUNITY VARCHAR2(10) NOT NULL
) ;


INSERT INTO VEHICLETRIPS values ( 1, 'VA01', to_date ('29-10-08 10:00', 'dd-mm-yy hh24:mi'), 1, 'Area 1' );
INSERT INTO VEHICLETRIPS values ( 2, 'VA01', to_date ('29-10-08 10:06', 'dd-mm-yy hh24:mi'), 1, 'Area 1' );
INSERT INTO VEHICLETRIPS values ( 3, 'VA01', to_date ('29-10-08 10:12', 'dd-mm-yy hh24:mi'), 1, 'Area 1' );
INSERT INTO VEHICLETRIPS values ( 4, 'VA01', to_date ('29-10-08 10:24', 'dd-mm-yy hh24:mi'), 1, 'Area 2' );
INSERT INTO VEHICLETRIPS values ( 5, 'VA01', to_date ('29-10-08 10:30', 'dd-mm-yy hh24:mi'), 1, 'Area 2' );
INSERT INTO VEHICLETRIPS values ( 6, 'VA01', to_date ('29-10-08 10:36', 'dd-mm-yy hh24:mi'), 1, 'Area 3' );
INSERT INTO VEHICLETRIPS values ( 7, 'VA01', to_date ('29-10-08 10:42', 'dd-mm-yy hh24:mi'), 1, 'Area 1' );
INSERT INTO VEHICLETRIPS values ( 8, 'VA01', to_date ('29-10-08 10:48', 'dd-mm-yy hh24:mi'), 1, 'Area 1' );

INSERT INTO VEHICLETRIPS values ( 9, 'VA01', to_date ('29-10-08 11:00', 'dd-mm-yy hh24:mi'), 2, 'Area 3' );
INSERT INTO VEHICLETRIPS values ( 10, 'VA01', to_date ('29-10-08 11:06', 'dd-mm-yy hh24:mi'), 2, 'Area 3' );
INSERT INTO VEHICLETRIPS values ( 11, 'VA01', to_date ('29-10-08 11:12', 'dd-mm-yy hh24:mi'), 2, 'Area 2' );
INSERT INTO VEHICLETRIPS values ( 12, 'VA01', to_date ('29-10-08 11:18', 'dd-mm-yy hh24:mi'), 2, 'Area 2' );
It is the places a vehicle has visited in two trips and the time at which it arrived to that place, so the order of records is very important. What i want to retrieve is a unique list of places this vehicle has visited and in the order they were visited in.

so if I use this query
QUERY
---------------
select row_number() over (partition by tripID order by tripID) id,
to_char (min(deviceDate), 'hh24:mi') StartTime ,
to_char(max(DeviceDate) , 'hh24:mi') STOPTime,
TripID, COmmunity
from vehicleTRips
group by tripID, community
/
the return value is as follows
RESULT
------
ID START STOPT TRIPID COMMUNITY
---------- ----- ----- ---------- ----------
1 10:00 10:48 1 Area 1
2 10:24 10:30 1 Area 2
3 10:36 10:36 1 Area 3
1 11:12 11:18 2 Area 2
2 11:00 11:06 2 Area 3
and what i want is as follows...
DESIRED RESULT
--------------
ID START STOPT TRIPID COMMUNITY
---------- ----- ----- ---------- ----------
1 10:00 10:12 1 Area 1
2 10:24 10:30 1 Area 2
3 10:36 10:36 1 Area 3
4 10:42 10:48 1 Area 1

1 11:12 11:18 2 Area 2
2 11:00 11:06 2 Area 3
so can anyone help me find the query that will generate the above result!

Thanks in advance...
Newbie
 
Join Date: Oct 2008
Location: Lebanon
Posts: 2
#2: Oct 29 '08

re: Group consecutive records


Ok got an answer for this one... here is it for those of you who come across this post...

Expand|Select|Wrap|Line Numbers
  1. select to_char(min(deviceDate), 'hh24:mi:ss'), community from 
  2. (
  3.     select devicedate, community,
  4.            max(rn) 
  5.              over(order by devicedate) max_rn
  6.       from (
  7.         select devicedate, community,
  8.                case
  9.                when not (COMMUNITY = LCOMMUNITY) or lcommunity is null
  10.                then row_num
  11.                 end rn
  12.           from (
  13.         select devicedate,  community,
  14.                lag(COMMUNITY)
  15.                   over (order by deviceDate) lCOMMUNITY,
  16.                row_number()
  17.                   over (order by deviceDate) row_num
  18.           from vehicletrips
  19.            )
  20.     )
  21. ) group by max_rn, community
Reply