2013-08-27 69 views
2

我试图更新一个存储过程,该过程确定从接收票证时的响应时间。在表格中,我有收到票证时的时间戳(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 
+0

每星期一到星期五的9个小时算不算容易吗? – dcaswell

回答

0

我喜欢在表格中存储重要的业务数据。像这样的查询

select min(cal_date), 
     max(cal_date), 
     sum(hours_open) total_time_open, 
     sum(hours_closed) total_time_closed 
from daily_hours_open_and_closed 
where cal_date between '2013-08-28' and '2013-09-03'; 

当它们基于存储在简单表格中的数据时,易于理解,维护和调试。

我会从calendar table开始,并为您的位置打开时添加一张表。该表“open_times”是最简单的开始方式,但对您的业务来说可能太简单了。例如,您可能需要更严格的CHECK约束。此外,我没有试图做到这一点,尽管最终查询在我的开发框中仅运行了12毫秒。

create table open_times (
    bus_open timestamp primary key, 
    bus_closed timestamp not null 
    check (bus_closed > bus_open) 
); 

快速和肮脏的方式来填充平日小时该表于2013年

with openings as (
    select generate_series(timestamp '2013-01-01 09:00', 
         timestamp '2013-12-31 18:00', '1 day') bus_open 
) 
insert into open_times 
select bus_open, bus_open + interval '9 hours' bus_closed 
from openings 
where extract(dow from bus_open) between 1 and 5 
order by bus_open; 

劳动节是这里的节日,所以周一,09月02日是一个节日。删除2013-09-02。

delete from open_times 
where bus_open = '2013-09-02 09:00'; 

这是我很感兴趣的展示它是如何工作的目的,唯一的假期。当然,你必须比我做得更好。

为了使事情变得更简单,创建一个视图,以每天的间隔显示每天的操作时间。

create view daily_hours_open_and_closed as 
select c.cal_date, 
     ot.bus_open, 
     ot.bus_closed, 
     coalesce(bus_closed - bus_open, interval '0 hours') as hours_open, 
     interval '24 hours' - (coalesce(bus_closed - bus_open, interval '0 hours')) as hours_closed 
from calendar c 
left join open_times as ot 
     on c.cal_date = cast(ot.bus_open as date); 

现在,有多少时间是我们开多少小时都是我们关闭了2013年8月28日和2013年9月3日之间的7天?原始数据的查询现在已经很简单了。

select * 
from daily_hours_open_and_closed 
where cal_date between '2013-08-28' and '2013-09-03' 
order by cal_date; 

cal_date  bus_open    bus_closed   hours_open hours_closed 
-- 
2013-08-28 2013-08-28 09:00:00 2013-08-28 18:00:00 09:00:00 15:00:00 
2013-08-29 2013-08-29 09:00:00 2013-08-29 18:00:00 09:00:00 15:00:00 
2013-08-30 2013-08-30 09:00:00 2013-08-30 18:00:00 09:00:00 15:00:00 
2013-08-31            00:00:00 24:00:00 
2013-09-01            00:00:00 24:00:00 
2013-09-02            00:00:00 24:00:00 
2013-09-03 2013-09-03 09:00:00 2013-09-03 18:00:00 09:00:00 15:00:00 

使用集合函数进行算术运算。

select min(cal_date), 
     max(cal_date), 
     sum(hours_open) total_time_open, 
     sum(hours_closed) total_time_closed 
from daily_hours_open_and_closed 
where cal_date between '2013-08-28' and '2013-09-03' 

min   max   total_time_open total_time_closed 
-- 
2013-08-28 2013-09-03 36:00:00   132:00:00 
0

你可以通过这样的查询次数,周末试穿sql fiddle demo

或者如果你的日期可能有不同的年份:

select 
    *, 
    trunc(date_part('day', ref_dttm - first_action_dttm)/7) * 2 + 
    case extract(dow from first_action_dttm) when 0 then 1 when 6 then 2 else 0 end + 
    case extract(dow from ref_dttm) when 6 then 1 when 0 then 2 else 0 end - 
    case when extract(dow from ref_dttm) = extract(dow from first_action_dttm) then 2 else 0 end as weekends 
from t_tickets 

试试sql fiddle demo

0

您可以使用generate_series来计算区间周六和周日的数量:

-- sample data 
with t as (
    (select 1 as id, '2012-01-01'::timestamp as tstart, '2012-02-01'::timestamp as tend) union all -- 9 
    (select 2 as id, '2011-12-31'::timestamp as tstart, '2012-02-04'::timestamp as tend) union all -- 11 
    (select 3 as id, '2011-12-30'::timestamp as tstart, '2012-02-05'::timestamp as tend) union all -- 12 
    (select 4 as id, '2011-12-30'::timestamp as tstart, '2012-02-07'::timestamp as tend)   -- 12 
) 

-- Calculate number of weekend days 

select 
    id, 
    sum((dow not between 1 and 5)::int) number_of_weekend_days 
from 
    (select id, extract(dow from generate_series(tstart,tend,'1 day')) as dow from t) x 
group by 
    id 

我认为这将是很慢的,如果你有大量的数据。

相关问题