Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I've vehicle tracking data, I want to create Vehicle stoppage report. Help me to write sql query for that. Actual Data and resultant data are like Table1 and Table2 respectively.

TABLE 1
CSS
VehicleId   UnixTimeStamp           Speed
1           17-01-2015 12:00:05       12
1           17-01-2015 12:00:06       0
1           17-01-2015 12:00:07       0
1           17-01-2015 12:00:08       0
1           17-01-2015 12:00:09       5
1           17-01-2015 12:00:10       8
1           17-01-2015 12:00:11       0
1           17-01-2015 12:00:12       0
1           17-01-2015 12:00:13       0
1           17-01-2015 12:00:14       7
2           17-01-2015 12:00:05       10
2           17-01-2015 12:00:06       0
2           17-01-2015 12:00:07       0
2           17-01-2015 12:00:08       0
2           17-01-2015 12:00:09       12
2           17-01-2015 12:00:10       0
2           17-01-2015 12:00:11       0
2           17-01-2015 12:00:12       10


TABLE 2
CSS
Vehicield	Stopped From	                 Stopped To	        Duration (Sec)
1	        17-01-2015 12:00:06	         17-01-2015 12:00:08	         3
1	        17-01-2015 12:00:11	         17-01-2015 12:00:13	         3
2	        17-01-2015 12:00:06	         17-01-2015 12:00:08	         3
2	        17-01-2015 12:00:10	         17-01-2015 12:00:11	         2
Posted
Updated 17-Jan-15 1:56am
v2
Comments
Wendelius 17-Jan-15 7:48am    
There's your data but what do you want to select? Please provide an example of the desired output
Mohd Arshad Malik 17-Jan-15 7:56am    
Desired output is in Table2.

Haven't actually tested this but perhaps something like this:
SQL
select start.unixtimestamp,
       stop.unixtimestamp
from table1 start,
     table1 stop
where start.speed        = 0
and   stop.vehicleid     = start.vehicleid
and   stop.unixtimestamp > start.unixtimestamp
and   stop.speed         = 0
and   exists (select 1 
              from  table1 prev
              where prev.vehicleid     = start.vehicleid
              and   prev.unixtimestamp = (select max(p2.unixtimestamp)
                                          from   table1 p2
                                          where  p2.vehicleid     = start.vehicleid
                                          and    p2.unixtimestamp < start.unixtimestamp)
              and   prev.speed > 0) 
and stop.unixtimestamp = (select max(p4.unixtimestamp)
                          from table1 p4
						  where p4.unixtimestamp < (select min(p3.unixtimestamp)
                                 from   table1 p3
                                 where  p3.vehicleid     = stop.vehicleid
                                 and    p3.unixtimestamp > start.unixtimestamp
                                 and    p3.speed         != 0))


EDIT: corrected few mistakes
 
Share this answer
 
v2
Comments
Mohd Arshad Malik 18-Jan-15 0:06am    
Thanks a lot for the solution, but another problem with me is: I've to use this query on a very big datatable where lacs of new records are being generated everyday. On that table response time of query is in minutes.

Can you suggest anything so I could get results in optimal time.

Thanks & Regards
Wendelius 18-Jan-15 9:47am    
Yep, indexing. Proper indexing is the key to fast access. Also you have to consider limiting the time frame. Do you always calculate all the starts and stops from all time beginning or for example only for last week or so.
I tried this...

SQL
declare @t as table (vehicleid INT, unixtimestamp DATETIME, speed int)

INSERT INTO @t VALUES(1, '2015-01-17 12:00:05', 12)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:06', 0)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:07', 0)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:08', 0)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:09', 5)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:10', 8)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:11', 0)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:12', 0)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:13', 0)
INSERT INTO @t VALUES(1, '2015-01-17 12:00:14', 7)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:05', 10)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:06', 0)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:07', 0)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:08', 0)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:09', 12)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:10', 0)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:11', 0)
INSERT INTO @t VALUES(2, '2015-01-17 12:00:12', 10)


select start.unixtimestamp,
       stop.unixtimestamp
from @t start,
     @t stop
where start.speed        = 0
and   stop.vehicleid     = start.vehicleid
and   stop.unixtimestamp > start.unixtimestamp
and   stop.speed         = 0
and   exists (select 1 
              from  @t prev
              where prev.vehicleid     = start.vehicleid
              and   prev.unixtimestamp = (select max(p2.unixtimestamp)
                                          from   @t p2
                                          where  p2.vehicleid = start.vehicleid
                                          and    p2.unixtimestamp < start.unixtimestamp)
              and   prev.speed > 0) 
and not exists (select 1
                from   @t p3
                where  p3.vehicleid     = start.vehicleid
                and    p3.unixtimestamp > start.unixtimestamp
                and    p3.unixtimestamp < stop.unixtimestamp
                and    p3.speed         > 0)



but results are not correct...two columns like this:

CSS
unixtimestamp           unixtimestamp
2015-01-17 12:00:06.000 2015-01-17 12:00:07.000
2015-01-17 12:00:06.000 2015-01-17 12:00:08.000
2015-01-17 12:00:11.000 2015-01-17 12:00:12.000
2015-01-17 12:00:11.000 2015-01-17 12:00:13.000
2015-01-17 12:00:06.000 2015-01-17 12:00:07.000
2015-01-17 12:00:06.000 2015-01-17 12:00:08.000
2015-01-17 12:00:10.000 2015-01-17 12:00:11.000
 
Share this answer
 
Comments
Wendelius 17-Jan-15 8:27am    
Modified the query a bit.

Don't comments to answers as new solutions. Instead, use the "Have a Question or Comment" button.
King Fisher 17-Jan-15 8:28am    
Remove this Post and kindly use Comment Widget for your Queries..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900