I'm trying to update a stored procedure that determines a response time from when a ticket is received. In the table I have the timestamp when the ticket was received (ref_dttm TIMESTAMP WITHOUT TIME ZONE) and the timestamp when the ticket was first responded to (first_action_dttm TIMESTAMP WITHOUT TIME ZONE). When calculating the response time, I need to account for operating hours, weekends, and their holiday closures.
Currently the function calculates the interval and can subtract the hours they business is closed but I can't seem to figure out a way to exclude the weekends and holidays. Basically I'll need to subtract 15 hrs per week day (open 0900-1800) and 24 hrs for each weekend day and holiday.
Given the day of the week that the ticket is received and the time span:
Select
extract(dow from ref_dttm) as dow,
extract(days from (ref_dttm - first_action_dttm) as days
Is there a simple way to determine how many weekends have passed?
This is what I have so far - it subtracts 15 hrs per day and doesn't account for weekends:
CREATE TEMP TABLE tmp_ticket_delta ON COMMIT DROP AS
SELECT id,ticket_id,ticket_num
,(ticket_dttm - first_action_dttm) as delta
,extract(days from (ticket_dttm - first_action_dttm)) as days
,ticket_descr
FROM t_tickets
WHERE ticket_action_by > 0
SELECT id,ticket_id,ticket_num,delta,days,ticket_descr,
CASE WHEN days = 0 THEN
CASE WHEN extract(hour from delta) > 15 THEN
--less than one day but outside of business hours so subtract 15 hrs
delta - INTERVAL '15:00:00.000'
ELSE
delta
END
ELSE
CASE WHEN extract(hour from delta) > 15 THEN
--take the total number of hours - closing hours + delta - closed hours
(((days * 24) - (days * 15)) * '1 hour'::INTERVAL) + delta - INTERVAL '15:00:00.000' - (days * '1 day'::INTERVAL)
ELSE
(((days * 24) - (days * 15)) * '1 hour'::INTERVAL) + delta - (days * '1 day'::INTERVAL)
END
END AS adj_diff
FROM tmp_ticket_delta