SQLite Forum

Calculating Trip Duration

Calculating Trip Duration

(1) By David Jackson (davidjackson) on 2021-07-28 22:58:14 [link] [source]

How can I use a case statement to calculate trip duration?

end_datetime - start_endtime = trip duration. If the trip duration is less than 0 then I want to do: start_endtime - end_datetime = trip duration.

Here is the code I'm trying to use:

Select case when datediff(minute, started_at, ended_at) < 0 then datediff(minute, ended_at, started_at) else datediff(minute, started_at, ended_at) end

FROM detail

Thanks, David

(2) By Warren Young (wyoung) on 2021-07-28 23:26:27 in reply to 1 [source]

Why not just write abs(datediff(...))? See the docs.

…and then ask why you've got your start and end times swapped.