我試圖更新一個存儲過程,該過程確定從接收票證時的響應時間。在表格中,我有收到票證時的時間戳(ref_dttm TIMESTAMP WITHOUT TIME ZONE)和第一次響應票證時的時間戳(first_action_dttm TIMESTAMP WITHOUT TIME ZONE)。在計算響應時間時,我需要考慮運營時間,週末和假期關閉。Postgres函數來確定一個時間間隔內的週末數
目前該函數計算的時間間隔,可以減去他們的業務關閉的時間,但我似乎無法找出排除週末和節假日的方法。基本上我需要在每個週末日和假期中每週減去15小時(打開0900-1800)和24小時。
鑑於票收到星期和時間跨度:
Select
extract(dow from ref_dttm) as dow,
extract(days from (ref_dttm - first_action_dttm) as days
有沒有一種簡單的方法來確定有多少個週末已經過去了?
這是我迄今爲止 - 它減去15個小時每天,不考慮週末:
select
*,
(extract(week from ref_dttm) - extract(week from first_action_dttm)) * 2 -
case extract(dow from first_action_dttm) when 0 then 1 else 0 end +
case extract(dow from ref_dttm) when 0 then 2 when 6 then 1 else 0 end
from t_tickets
:
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
每星期一到星期五的9個小時算不算容易嗎? – dcaswell