从时间段中删除假期间隔

时间:2014-01-02 15:17:02

标签: sql intervals teradata period hour

以下查询计算收到请求和响应请求之间已经过的工时。工作时间为周一至周五9点至5点。

select tmp.Request_Id, 
sum (tmp.hhmmss) as WorkHoursElapsed
from

    (select Request_Id,
    period(cast(c.calendar_date as timestamp(6)) + INTERVAL '0000 09:00:00.00000' DAY TO SECOND,cast(calendar_date as timestamp(6)) + INTERVAL '0000 17:00:00.000000' DAY TO SECOND) as p, 
    p P_INTERSECT b.ref as i, 
    (end(i) - begin(i)) Hour(4) to MINUTE as hhmmss 
    from calendar c 
    Cross join

        (select Request_Id, period(LocalTime_Received_RTA ,LocalTime_Responded_RTA) as ref 
        from Responded_Requests 
         WHERE LocalTime_Received_RTA<LocalTime_Responded_RTA 
        group by 1,2
        ) as b 

    where p overlaps b.ref
    and c.day_of_week between 2 and 6  

    ) as tmp

group by 1

我有另一张假期表,包括开始和结束间隔

开始时间:2014年1月1日上午12:00:00结束时间:2014年1月1日下午11:59:00

如何加入假期表以排除假期间隔?

1 个答案:

答案 0 :(得分:0)

您将需要转换它Teradata SQL systax,在T-SQL中,您只需添加另一个连接并排除这样的值

SELECT m.*
    FROM mytable AS m
    JOIN othertable AS o
        ON m.date NOT BETWEEN o.StartDate AND o.EndDate

SELECT m.*
    FROM mytable AS m
    JOIN othertable AS o
        ON m.date < o.StartDate
           AND m.date > o.EndDate