计算oracle中的班次时间

时间:2013-12-10 09:25:20

标签: oracle date

我有两张桌子。一个具有某些工作的开始日期和结束日期,另一个具有班次时间表。这是Shift表的结构

职位表

Job_ID            Start_Time          End_Time
1           01.12.2013 16:38:56     09.12.2013 10:38:56
2           11.11.2013 10:14:13     07.12.2013 12:28:26
3           29.11.2013 08:20:22     05.12.2013 07:18:17

转换数据表

Start      End        Day
    0:00    6:00:00   Monday
 6:00:00   12:00:00   Monday
12:00:00    0:00:00   Tuesday
 0:00:00    6:00:00   Wednesday
 6:00:00   12:00:00   Saturday
 6:00:00   12:00:00   Sunday

预期输出

Job_ID            Start_Time          End_Time             Shift Timings
1           01.12.2013 16:38:56     09.12.2013 10:38:56      1   18:00:00

例如:从'01 .12.2013 16:38:56'开始的时间工作开始直到'09 .12.2013 10:38:56'关闭,共有42小时的班次时间(计算得出)根据槽表,作为间隔值)。我希望我的要求清楚。

对于具有开始时间和结束时间的作业条目的每一行,我需要计算涉及多少班次时间。

1 个答案:

答案 0 :(得分:0)

这个问题的一个解决方案是:

1)作业是一个时间间隔,您需要每天生成1个元组。如果工作从今天开始到明天结束,则需要2个元组(今天和明天的日期)。你可以这样做:

SELECT JOBID, 
    GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) JOBDAYSTART,
    LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD')) JOBDAYEND, 
    NDAY 
FROM (
    SELECT LEVEL NDAY 
    FROM DUAL 
    CONNECT BY LEVEL <= (SELECT CEIL(MAX(JOBEND - JOBSTART))+1 FROM JOBS)
    ), JOBS 
WHERE GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) <= 
    LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD'))

2)你必须修复SHIFT表(在一些元组结束时间为0:00:00,这是第二天的小时)。您可以使用:

SELECT SHIFTSTART, SHIFTEND, DAY, 0 SHIFTENDOFFSET
FROM SHIFTS
WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') 
    <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS')
UNION
SELECT SHIFTSTART, SHIFTEND, DAY, 1 SHIFTENDOFFSET
FROM SHIFTS
WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') 
    > TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS')

此选择包括SHIFTENDOFFSET :如果小时是在开始日,则为0;如果小时是在开始日之后的一天,则为1。

3)现在你可以使用SHIFTS的日期和“每个工作日”的日期加入,并过滤不可能的元组(在班次结束后作业开始的那些日子)

SELECT JOBID, 
    GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART), 
    LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND), 
    24*(LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)) TOTAL
FROM
    (SELECT JOBID, GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) JOBDAYSTART, LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD')) JOBDAYEND, NDAY FROM (SELECT LEVEL NDAY FROM DUAL CONNECT BY LEVEL <= (SELECT CEIL(MAX(JOBEND - JOBSTART))+1 FROM JOBS)), JOBS wHERE GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) <= LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD'))) 
    JOIN
    (SELECT SHIFTSTART, SHIFTEND, DAY, 0 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') UNION SELECT SHIFTSTART, SHIFTEND, DAY, 1 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') > TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS'))
    ON (TO_CHAR(JOBDAYSTART, 'Day','nls_date_language=english') LIKE (DAY||'%'))
WHERE LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND)
    > GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)
ORDER BY 1,2

4)按JOBID和SUM分组以获得总小时数。

SELECT JOBID, 
    24*SUM(LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)) TOTAL
FROM
    (SELECT JOBID, GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) JOBDAYSTART, LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD')) JOBDAYEND, NDAY FROM (SELECT LEVEL NDAY FROM DUAL CONNECT BY LEVEL <= (SELECT CEIL(MAX(JOBEND - JOBSTART))+1 FROM JOBS)), JOBS WHERE GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) <= LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD'))) 
    JOIN
    (SELECT SHIFTSTART, SHIFTEND, DAY, 0 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') UNION SELECT SHIFTSTART, SHIFTEND, DAY, 1 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') > TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS')) 
    ON (TO_CHAR(JOBDAYSTART, 'Day','nls_date_language=english') LIKE (DAY||'%'))
WHERE LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) > GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)
GROUP BY JOBID

5)最后,应用您期望的输出格式:

SELECT JOBID "Job_ID", JOBSTART "Start_Time", JOBEND "End_Time", TRUNC(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)))||' '|| TO_CHAR(TRUNC(MOD(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)),1)*24),'FM00')||':'|| TO_CHAR(TRUNC(MOD(MOD(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)),1)*24,1)*60),'FM00')||':'|| TO_CHAR(TRUNC(MOD(MOD(MOD(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)),1)*24,1)*60,1)*60),'FM00') "Shift Timings" FROM (SELECT JOBID, JOBSTART, JOBEND, GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) JOBDAYSTART, LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD')) JOBDAYEND, NDAY FROM (SELECT LEVEL NDAY FROM DUAL CONNECT BY LEVEL <= (SELECT CEIL(MAX(JOBEND - JOBSTART))+1 FROM JOBS)), JOBS WHERE GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) <= LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD'))) JOIN (SELECT SHIFTSTART, SHIFTEND, DAY, 0 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') UNION SELECT SHIFTSTART, SHIFTEND, DAY, 1 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') > TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') ) ON (TO_CHAR(JOBDAYSTART, 'Day','nls_date_language=english') LIKE (DAY||'%')) WHERE LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) > GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART) GROUP BY JOBID, JOBSTART, JOBEND

在您的示例中,您说工作1累计42小时,但这不正确。工作1有超过52小时。工作3有42个小时。

您可以使用以下脚本(它创建JOBS和SHIFTS表,介绍您的样本行并执行3),4)和5)查询):

ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS DAY';

DROP TABLE JOBS;

DROP TABLE SHIFTS;

CREATE TABLE JOBS
 (JOBID NUMBER, JOBSTART DATE, JOBEND DATE);

CREATE TABLE SHIFTS
 (SHIFTSTART VARCHAR2(8), SHIFTEND VARCHAR2(8), DAY VARCHAR2(13));

INSERT INTO JOBS VALUES (1, TO_DATE('01.12.2013 16:38:56','DD.MM.YYYY HH24:MI:SS'), TO_DATE('09.12.2013 10:38:56','DD.MM.YYYY HH24:MI:SS'));

INSERT INTO JOBS VALUES (2, TO_DATE('11.11.2013 10:14:13','DD.MM.YYYY HH24:MI:SS'), TO_DATE('07.12.2013 12:28:26','DD.MM.YYYY HH24:MI:SS'));

INSERT INTO JOBS VALUES (3, TO_DATE('29.11.2013 08:20:22','DD.MM.YYYY HH24:MI:SS'), TO_DATE('05.12.2013 07:18:17','DD.MM.YYYY HH24:MI:SS'));


-- Fix the start hour to 0:00:00 instead of 0:00

INSERT INTO SHIFTS VALUES ('0:0:00','6:00:00','Monday');

INSERT INTO SHIFTS VALUES ('6:00:00','12:00:00','Monday');

INSERT INTO SHIFTS VALUES ('12:00:00','0:00:00','Tuesday');

INSERT INTO SHIFTS VALUES ('0:00:00','6:00:00','Wednesday');

INSERT INTO SHIFTS VALUES ('6:00:00','12:00:00','Saturday');

INSERT INTO SHIFTS VALUES ('6:00:00','12:00:00','Sunday');

COMMIT;

SELECT JOBID, GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'),
JOBDAYSTART), LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND), 24*(
LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'),
JOBDAYSTART)) TOTAL
FROM
(SELECT JOBID, GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) JOBDAYSTART, LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD')) JOBDAYEND, NDAY
FROM (SELECT LEVEL NDAY FROM DUAL CONNECT BY LEVEL <= (SELECT CEIL(MAX(JOBEND - JOBSTART))+1 FROM JOBS)), JOBS
WHERE GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) <= LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD'))) 
JOIN
(SELECT SHIFTSTART, SHIFTEND, DAY, 0 SHIFTENDOFFSET
FROM SHIFTS
WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS')
UNION
SELECT SHIFTSTART, SHIFTEND, DAY, 1 SHIFTENDOFFSET
FROM SHIFTS
WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') > TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS')
) 
ON (TO_CHAR(JOBDAYSTART, 'Day','nls_date_language=english') LIKE (DAY||'%'))
WHERE LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) > GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'),
JOBDAYSTART)
ORDER BY 1,2;

SELECT JOBID, 24*SUM(
LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'),
JOBDAYSTART)) TOTAL
FROM
(SELECT JOBID, GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) JOBDAYSTART, LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD')) JOBDAYEND, NDAY
FROM (SELECT LEVEL NDAY FROM DUAL CONNECT BY LEVEL <= (SELECT CEIL(MAX(JOBEND - JOBSTART))+1 FROM JOBS)), JOBS
WHERE GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) <= LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD'))) 
JOIN
(SELECT SHIFTSTART, SHIFTEND, DAY, 0 SHIFTENDOFFSET
FROM SHIFTS
WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS')
UNION
SELECT SHIFTSTART, SHIFTEND, DAY, 1 SHIFTENDOFFSET
FROM SHIFTS
WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') > TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS')
) 
ON (TO_CHAR(JOBDAYSTART, 'Day','nls_date_language=english') LIKE (DAY||'%'))
WHERE LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) > GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'),
JOBDAYSTART)
GROUP BY JOBID;

SELECT JOBID "Job_ID", JOBSTART "Start_Time", JOBEND "End_Time", TRUNC(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)))||'   '|| TO_CHAR(TRUNC(MOD(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)),1)*24),'FM00')||':'|| TO_CHAR(TRUNC(MOD(MOD(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)),1)*24,1)*60),'FM00')||':'|| TO_CHAR(TRUNC(MOD(MOD(MOD(SUM( LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) - GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART)),1)*24,1)*60,1)*60),'FM00') "Shift Timings" FROM (SELECT JOBID, JOBSTART, JOBEND, GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) JOBDAYSTART, LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD')) JOBDAYEND, NDAY FROM (SELECT LEVEL NDAY FROM DUAL CONNECT BY LEVEL <= (SELECT CEIL(MAX(JOBEND - JOBSTART))+1 FROM JOBS)), JOBS WHERE GREATEST(JOBSTART, TRUNC(JOBSTART+(NDAY-1),'DDD')) <= LEAST(JOBEND, TRUNC(JOBEND+NDAY,'DDD'))) JOIN (SELECT SHIFTSTART, SHIFTEND, DAY, 0 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') <= TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') UNION SELECT SHIFTSTART, SHIFTEND, DAY, 1 SHIFTENDOFFSET FROM SHIFTS WHERE TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS') > TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') ) ON (TO_CHAR(JOBDAYSTART, 'Day','nls_date_language=english') LIKE (DAY||'%')) WHERE LEAST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTEND,'YYYY-MM-DD HH24:MI:SS') + SHIFTENDOFFSET, JOBDAYEND) > GREATEST(TO_DATE(TO_CHAR(JOBDAYSTART,'YYYY-MM-DD ') || SHIFTSTART,'YYYY-MM-DD HH24:MI:SS'), JOBDAYSTART) GROUP BY JOBID, JOBSTART, JOBEND;