3

我正在尝试更新一个存储过程,该过程确定从收到票时起的响应时间。在表中,我有收到票时的时间戳(ref_dttm TIMESTAMP WITHOUT TIME ZONE)和第一次响应票时的时间戳(first_action_dttm TIMESTAMP WITHOUT TIME ZONE)。在计算响应时间时,我需要考虑营业时间、周末和假期休息时间。

目前,该函数计算间隔并可以减去他们的营业时间,但我似乎无法找到排除周末和节假日的方法。基本上我需要每周减少 15 小时(0900-1800 开放)和每个周末和节假日的 24 小时。

给定收到票的星期几和时间跨度:

Select 
    extract(dow from ref_dttm) as dow, 
    extract(days from (ref_dttm - first_action_dttm) as days

有没有一种简单的方法来确定已经过去了多少个周末?

这是我到目前为止所拥有的 - 它每天减去 15 小时,并且不考虑周末:

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
4

3 回答 3

1

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

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';

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

我会从一个日历表开始,然后在您的位置开放时添加一个表。此表“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;

劳动节在这里是假期,所以 9 月 2 日星期一是假期。删除 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
于 2013-08-27T13:09:17.873 回答
0

您可以通过以下查询来计算周末:

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

试试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

于 2013-08-27T08:25:05.040 回答
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

如果您有大量数据,我认为这将非常慢。

于 2013-08-27T08:25:20.420 回答