Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

SQL to calculate time difference between two different rows and columns

$
0
0
i have to calulate the total length of time driver travelled in each location each time.i am using sql server 2008 version
the DDL for this table is
DECLARE @TEMP TABLE (VEH_NO VARCHAR(10),DRIV_NO VARCHAR(5),LOC VARCHAR(5),START_DATETIME DATETIME,STOP_DATETIME DATETIME, TDIFF INT, SEQ INT)
INSERT INTO @TEMP VALUES
('JHA207','D361',	'CA',	'2012-07-17 14:56:00.000',	'2012-07-20 09:42:00.000',	4006,	17),
('JHA207','D361',	'OR',	'2012-07-20 09:42:00.000',	'2012-07-20 13:19:00.000',	217,	18),
('JHA207','D361',	'CA',	'2012-07-20 13:19:00.000',	'2012-07-20 13:45:00.000',	26,	19),
('JHA207','D361',	'CA',	'2012-07-20 13:45:00.000',	'2012-07-20 16:30:00.000',	165,	20),
('JHA207','D361',	'CA',	'2012-07-20 16:30:00.000',	'2012-07-21 15:20:00.000',	1370,	21),
('JHA207','D361',	'OR',	'2012-07-21 15:20:00.000',	'2012-07-22 18:10:00.000',	1610,	22),
('JHA207','D361',	'CA',	'2012-07-22 18:10:00.000',	'2012-07-23 07:29:00.000',	799,	23),
('JHA207','D584',	'GA',	'2012-10-09 13:01:00.000',	'2012-10-09 23:59:00.000',	658,	1),
('JHA207','D546',	'MO',	'2012-09-25 11:00:00.000',	'2012-09-25 23:59:00.000',	779,	1),
('JHA207','D654',	'NE',	'2012-09-25 11:00:00.000',	'2012-09-25 23:59:00.000',	779,	1),
('JHA207','D492',	'ATC',	'2012-10-27 03:02:00.000',	'2012-10-27 05:13:00.000',	131,	1),
('JHA207','D492',	'CA',	'2012-10-27 05:13:00.000',	'2012-10-27 05:57:00.000',	44,	2),
('JHA207','D492',	'CA',	'2012-10-27 05:57:00.000',	'2012-10-27 05:59:00.000',	2,	3),
('JHA207','D492',	'CA',	'2012-10-27 05:59:00.000',	'2012-11-07 09:49:00.000',	16070,	4),
('JHA207','D492',	'4PE',	'2012-11-07 09:49:00.000',	'2012-11-07 11:06:00.000',	77,	5),
('JHA207','D492',	'4PE',	'2012-11-07 11:06:00.000',	'2012-11-30 09:38:00.000',	33032,	6)
I want the result table should look like this
here the tricky part is in the result table 3rd row start_datetime is from #temp table's 3rd row's start_datetime and 
stop_datetime is from #temp table's 5th row's stop_datetime
these 3 rows 
('JHA207','D361',	'CA',	'2012-07-20 13:19:00.000',	'2012-07-20 13:45:00.000',	26,	19),
('JHA207','D361',	'CA',	'2012-07-20 13:45:00.000',	'2012-07-20 16:30:00.000',	165,	20),
('JHA207','D361',	'CA',	'2012-07-20 16:30:00.000',	'2012-07-21 15:20:00.000',	1370,	21),
becomes 1 row(like below) in result table
('JHA207','D361',	'CA',	'2012-07-20 13:19:00.000',	'2012-07-21 15:20:00.000',	1651,	19),
and the result table look like this
VEH_NO     DRIV_NO  LOC     START_DATETIME              STOP_DATETIME               		TDIFF  SEQ 
'JHA207','D361',	'CA',	'2012-07-17 14:56:00.000',	'2012-07-20 09:42:00.000',	4006	17
'JHA207','D361',	'OR',	'2012-07-20 09:42:00.000',	'2012-07-20 13:19:00.000',	217	18
'JHA207','D361',	'CA',	'2012-07-20 13:19:00.000',	'2012-07-21 15:20:00.000',	1651	19
'JHA207','D361',	'OR',	'2012-07-21 15:20:00.000',	'2012-07-22 18:10:00.000',	1610	20
'JHA207','D361',	'CA',	'2012-07-22 18:10:00.000',	'2012-07-23 07:29:00.000',	799	21
'JHA207','D584',	'GA',	'2012-10-09 13:01:00.000',	'2012-10-09 23:59:00.000',	658	1
'JHA207','D546',	'MO',	'2012-09-25 11:00:00.000',	'2012-09-25 23:59:00.000',	779	1
'JHA207','D654',	'NE',	'2012-09-25 11:00:00.000',	'2012-09-25 23:59:00.000',	779	1
'JHA207','D492',	'ATC',	'2012-10-27 03:02:00.000',	'2012-10-27 05:13:00.000',	131	1
'JHA207','D492',	'CA',	'2012-10-27 05:13:00.000',	'2012-11-07 09:49:00.000',	16116	2
'JHA207','D492',	'4PE',	'2012-11-07 09:49:00.000',	'2012-11-30 09:38:00.000',	33109	3
i would appriciate your help.
thanks


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>