简体   繁体   English

有什么方法可以使用DB2 SQL在表Workorder中查找重叠时间?

[英]Is there any way for finding the Overlapping Time in table Workorder using DB2 SQL?

I want to make a report using DB2 SQL that will show the list of duration (hours) between 15th and 17th May. 我想使用DB2 SQL进行报告,该报告将显示5月15日至17日之间的持续时间(小时)列表。 The current list look like this: 当前列表如下所示:

click here to view the list 点击这里查看列表

As you can see, from WO-391296 (second row) it has overlapping time in ACTSTART and ACTFINISH . 如您所见,从WO-391296 (第二行)开始,它在ACTSTARTACTFINISH有重叠的时间。

I've already tried the Overlaps function. 我已经尝试过重叠功能。 This is my query : 这是我的查询:

SELECT
    *
FROM
    workorder a,
    workorder b
WHERE
    a.wonum <> b.WONUM
    AND (a.ACTSTART, a.ACTFINISH) OVERLAPS (b.ACTSTART, b.ACTFINISH)
    AND DATE(a.ACTSTART) = '2019-05-15'
    AND DATE(a.ACTFINISH) = '2019-05-17'
    AND a.assetnum = 'A0000004'
    AND DATE(b.ACTSTART) = '2019-05-15'
    AND DATE(b.ACTFINISH) = '2019-05-17'
    AND b.assetnum = 'A0000004' ;

But it doesn't work and just giving me the error message.: 但这是行不通的,只是给我错误信息。

SQL Error [42601]: An unexpected token "OVERLAPS" was found following "TSTART, a.ACTFINISH)". SQL错误[42601]:在“ TSTART,a.ACTFINISH)”之后发现意外的标记“ OVERLAPS”。 Expected tokens may include: "IN".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.64.114 预期的令牌可能包括:“ IN” .. SQLCODE = -104,SQLSTATE = 42601,DRIVER = 3.64.114

Here's my actual full query : 这是我的实际完整查询:

SELECT assetnum, wonum, worktype, actstart, actfinish, 
    (DAY(ACTFINISH-ACTSTART)*24 + HOUR(ACTFINISH-ACTSTART)) + FLOAT(MINUTE(ACTFINISH-ACTSTART))/60 AS wotime 
FROM ( 
    SELECT assetnum, wonum, WORKTYPE, ACTSTART, 
        (CASE WHEN ACTFINISH IS NULL THEN '2019-05-17-24.00.00' ELSE ACTFINISH END) AS ACTFINISH 
    FROM workorder ast 
    WHERE istask=0 AND siteid = 'SBY' AND WORKTYPE = 'CM' AND
        (ast.assetnum = 'A0000004' or 
         assetnum in (select distinct assetnum from asset ast where ast.parent = 'A0000004') or 
         assetnum in (select distinct assetnum from asset ast where ast.parent 
                  in (select distinct assetnum from asset ast where ast.parent = 'A0000004'))) AND 
        ( 
            (status IN ('COMP', 'CONFIRM', 'CLOSE') AND 
            (date(ACTFINISH) BETWEEN '2019-05-15' and '2019-05-17')) OR 
            (status IN ('INPRG', 'COMP', 'CONFIRM', 'CLOSE') AND 
            (date(ACTSTART) <= '2019-05-17' AND (date(ACTFINISH) > '2019-05-17' OR ACTFINISH IS null))) 
        ) 
); 

The actual total duration of this list is 6 hours which is wrong , because I don't want to SUM the overlapping time. 该列表的实际总持续时间为6小时,这是错误的 ,因为我不想SUM重叠时间。

So, the final result must be 3.5 hours (without WO-391296 ). 因此,最终结果必须为3.5小时(不包括WO-391296 )。

2 Intervals overlap if: 2个间隔在以下情况下重叠:
start of the 1-st is <= of end of the 2-nd
and
start of the 2-nd is <= of end of the 1-st

create or replace function overlaps_dates (a_from date, a_to date, b_from date, b_to date)
returns int
deterministic
no external action
contains sql
return case when min(a_from, a_to) <= max(b_from, b_to) and min(b_from, b_to) <= max(a_from, a_to) then 1 else 0 end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM