Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to calculate total time taken in driving excluding time taken in Pause and Resume.

Sample Values are as below mention:

LocationTrackingId PatientRequestId	NurseId	LocationDateTime	Status<br />
          1	           22	                     4	17-06-2019 14:10	StopDriving<br />
          2	           22	                     4	17-06-2019 14:10	Driving<br />
          3	           22	                     4	17-06-2019 14:09	Driving<br />
          4	           22	                     4	17-06-2019 14:09	ResumeDriving<br />
          5	           22	                     4	17-06-2019 14:09	PauseDriving<br />
          6	           22	                     4	17-06-2019 14:08	Driving<br />
          7	           22	                     4	17-06-2019 14:07	Driving<br />
          8	           22	                     4	17-06-2019 14:06	Resumeriving<br />
          9	           22	                     4	17-06-2019 13:05	PauseDriving<br />
          10	           22	                     4	17-06-2019 13:04	Driving<br />
          11	           22	                     4	17-06-2019 13:03	Driving<br />
          12	           22	                     4	17-06-2019 13:02	StartDriving<br />
</blockquote>


What I have tried:

convert(varchar(5),DateDiff(s, pr.DrivingStartTime, pr.DrivingStopTime)/86400)+' Days '+convert(varchar(5),DateDiff(s, pr.DrivingStartTime, pr.DrivingStopTime)% 86400/3600)+':'+convert(varchar(5),DateDiff(s, pr.DrivingStartTime, pr.DrivingStopTime)%3600/60)+':'+convert(varchar(5),(DateDiff(s, pr.DrivingStartTime, pr.DrivingStopTime)%60)) AS TotalTravelTime  
Posted
Updated 24-Oct-19 1:33am
Comments
OriginalGriff 23-Oct-19 5:10am    
The query you show bears no relation at all to the data you have given: we can't work out from two disparate elements what the problem you are having is ... I suspect there is an intermediate step that you haven't given us.

Use the "Improve question" widget to edit your question and provide better information.

1 solution

Try this (may require tweaking)...
SQL
;WITH cte AS
(
    SELECT PatientRequestID, 
           MIN(LocationDateTime) AS StartTime, 
           MAX(LocationDateTime) AS EndTime, 
           DateDiff(second, MIN(LocationDateTime), MAX(LocationDateTime)) AS TotalSeconds
    FROM   dbo.MyTable
    GROUP  BY PatientRequestID
)
SELECT PatientRequestID, 
       StartTime, 
       EndTime, 
       TotalSeconds / 3600 AS Hours, 
       (TotalSeconds % 3600) / 60 AS Minutes, 
       TotalSeconds % 60 as Seconds,
       RIGHT('00', CAST(TotalSeconds / 3600 AS VARCHAR)+':'+
         RIGHT('00', CAST((TotalSeconds % 3600) / 60 AS VARCHAR)+':'+
         RIGHT('00', CAST(TotalSeconds % 60 AS VARCHAR) AS TotalTravelTime
FROM   CTE;
 
Share this answer
 
v2

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