I am trying to find the time difference in minutes between two dates, but I want the difference to account for the time the shop was open. The shop is open only 9 hours a day and only on weekdays (weekends are closed too), thus the time difference should account for the time as per the operating hours.
Example: 7/18/2019 9:50 AM - 7/22/2019 10:02 AM
SELECT date_diff('minute', date_parse('7/18/2019 9:50:00 AM','%m/%d/%Y %h:%i:%s %p'), date_parse('7/22/2019 10:02:00 AM','%m/%d/%Y %h:%i:%s %p'))
This gives the answer as 5772. But I want the answer to be 1092.
The excel formula is
A1 = 7/18/2019 9:50 AM
B1 = 7/22/2019 10:02 AM
if(OR(A1="",A1="NULL",B1="",B1="NULL"),"",(A1-B1)*24*60 - (days(A1,B1)+1-NETWORKDAYS(A1,B1))*24*60 - (NETWORKDAYS(A1,B1)-1)*15*60)
This version is working for me, but is there a better way? Also, if we could write a function on presto, I am unaware of how to write a callable function on presto.
with working_days AS
(
select count(*) as wd from unnest(sequence (cast('2017-06-07' as date), cast('2017-06-13' as date))) t(x)
WHERE extract (day_of_week from x) < 6
)
select date_diff('minute', cast('2017-06-07 11:19:11.287' as timestamp), cast('2017-06-13 09:53:14.750' as timestamp)) -
((date_diff('day', cast('2017-06-07' as date), cast('2017-06-13' as date))+1 - wd)*24*60 + (wd - 1)*16*60)
FROM working_days