I’m writing a query into a table that keeps track of a salesman’s status, and keeps track of when they travel. For reference, here are the statuses important to this question:
0: initial value, is working
600: traveling to break
601: on break
603: traveling to lunch
604: at lunch
I’m trying to calculate the total time an employee spends traveling. I am able to return the time to travel and time to lunch using
SELECT (cast(c1.status_change_dt as date) - cast(c2.status_change_dt as date)) as LunchTravel, C1.CREW_ID
FROM CREW_STATUS c1, CREW_STATUS c2
WHERE c1.status_id = '603' and c2.status_id = '602'
and c1.mobile_token = c2.mobile_token
With the same for the break. Mobile_token is to make sure that it’s being returned on the same day (each employee gets a unique token every day).
Status_change_dt is a timestamp
I do the same thing, but the other status_id’s to get the BreakTravel time.
So I end up with LunchTravel and BreakTravel, but I feel that creating 4 Crew_Status is inefficient, and it actually runs pretty slow.
Is there a better way to do this?
If I understand your question, I think you can use a UNION ALL, or a CASE statement.
OR