SQL根据连续的任务获取班次开始和班次结束

时间:2018-09-28 04:13:58

标签: sql-server recursive-query

我需要根据轮班中的任务获取轮班的真实长度。没有每个班次的开始和结束的轮班开始和结束时间的记录。我一直在尝试获取轮班开始的时间和轮班结束的时间。或以分钟为单位的班次长度。

|----|-------------|-----------|-------------------|-------------------|-----------|--------|  
| ID | EMPLOYEE_ID | TASK_TYPE |        START_TIME |          END_TIME | START_DAY | LENGTH |
|----|-------------|-----------|-------------------|-------------------|-----------|--------|
|  1 |       12344 |    TASK A | 28-Sep-2018 11:00 | 28-Sep-2018 12:00 |     43371 |     60 |
|  2 |       12344 |    TASK C | 28-Sep-2018 12:00 | 28-Sep-2018 19:00 |     43371 |    420 |
|  3 |      457547 |    TASK C | 28-Sep-2018 19:00 | 28-Sep-2018 21:00 |     43371 |    120 |
|  4 |      457547 |    TASK F | 28-Sep-2018 21:00 | 28-Sep-2018 23:00 |     43371 |    120 |
|  5 |      457547 |    TASK C | 28-Sep-2018 23:00 | 29-Sep-2018 02:00 |     43371 |    180 |
|  6 |       12344 |    TASK A | 30-Sep-2018 08:00 | 30-Sep-2018 14:00 |     43373 |    360 |
|----|-------------|-----------|-------------------|-------------------|-----------|--------|

下面的SQL字符串创建上面的表。

SELECT quintiq_id, 
   employee_id, 
   task_type, 
   Cast(start_time AS DATETIME)               AS START_TIME, 
   Cast(end_time AS DATETIME)                 AS END_TIME, 
   Cast(Cast(start_time AS DATE) AS DATETIME) AS START_DAY, 
   Datediff(minute, start_time, end_time)     AS LENGTH 
FROM   daysheet_active_shift 
WHERE  ( NOT task_type = 'OFF' ) 
   AND ( NOT task_type = 'OFF*' ) 
   AND employee_id <> 0; 

对task_type OFFOFF*进行过滤非常重要,因为它们将所有任务链接在一起,因此每个班次之间都有一个开始和结束时间。

我仅具有对数据库后端的读取访问权限,并且只有Access 2013可以将查询作为传递查询来运行。我试图将上面的查询转换为递归查询,以获取移位的总长度。但是我根本无法运行它。是的,累积查询确实可以在服务器上工作。有什么想法吗?

WITH L AS (
       SELECT QUINTIQ_ID,
                    EMPLOYEE_ID, 
                    TASK_TYPE, 
                    START_TIME, 
                    END_TIME, 
                    datediff(minute, START_TIME, END_TIME) as LENGTH, 
                    0 as TOT 
       FROM DAYSHEET_ACTIVE_SHIFT 
       WHERE QUNITIQ_ID IN (
              SELECT A.QUINTIQ_ID
              FROM DAYSHEET_ACTIVE_SHIFT AS A LEFT JOIN DAYSHEET_ACTIVE_SHIFT AS B ON
                     (A.EMPLOYEE_ID = B.EMPLOYEE_ID) And (A.START_TIME = B.END_TIME)
              WHERE B.QUINTIQ_ID IS NOT NULL 
                            AND (NOT A.TASK_TYPE = 'OFF') 
                            AND (NOT A.TASK_TYPE = 'OFF*') 
                            AND A.EMPLOYEE_ID <> 0
              )
      UNION ALL

      SELECT C.QUINTIQ_ID,
                    C.EMPLOYEE_ID, 
                    C.TASK_TYPE, 
                    C.START_TIME, 
                    C.END_TIME, 
                    datediff(minute, C.START_TIME, C.END_TIME) as LENGTH, 
                    C.TOT + D.TOT as TOT 
      FROM  DAYSHEET_ACTIVE_SHIFT AS C JOIN L AS D ON 
                    (C.EMPLOYEE_ID = D.EMPLOYEE_ID) And (D.START_TIME = 
C.END_TIME)
       )

SELECT * FROM L

我希望查询产生什么:

|----|-------------|-----------|-------------------|-------------------|-----------|--------|-----|  
| ID | EMPLOYEE_ID | TASK_TYPE |        START_TIME |          END_TIME | START_DAY | LENGTH | TOT |
|----|-------------|-----------|-------------------|-------------------|-----------|--------|-----|
|  1 |       12344 |    TASK A | 28-Sep-2018 11:00 | 28-Sep-2018 12:00 |     43371 |     60 | 480 |
|  2 |       12344 |    TASK C | 28-Sep-2018 12:00 | 28-Sep-2018 19:00 |     43371 |    420 | 480 |
|  3 |      457547 |    TASK C | 28-Sep-2018 19:00 | 28-Sep-2018 21:00 |     43371 |    120 | 420 |
|  4 |      457547 |    TASK F | 28-Sep-2018 21:00 | 28-Sep-2018 23:00 |     43371 |    120 | 420 |
|  5 |      457547 |    TASK C | 28-Sep-2018 23:00 | 29-Sep-2018 02:00 |     43371 |    180 | 420 |
|  6 |       12344 |    TASK A | 30-Sep-2018 08:00 | 30-Sep-2018 14:00 |     43373 |    360 | 360 |

|  7 |       12344 |    TASK A | 02-Oct-2018 06:00 | 02-Sep-2018 14:00 |     43375 |    480 | 480 |

|  8 |       12344 |    TASK A | 02-Oct-2018 23:00 | 03-Oct-2018 06:00 |     43375 |    420 | 420 |

|  9 |       12344 |    TASK A | 06-Oct-2018 08:00 | 06-Oct-2018 09:00 |     43379 |     60 | 420 |
| 10 |       12344 |    TASK B | 06-Oct-2018 09:00 | 06-Oct-2018 15:00 |     43379 |    360 | 420 |

| 11 |       12344 |    TASK A | 06-Oct-2018 22:00 | 07-Oct-2018 04:00 |     43379 |    360 | 480 |
| 12 |       12344 |    TASK A | 07-Oct-2018 04:00 | 06-Oct-2018 06:00 |     43380 |    120 | 480 |
|----|-------------|-----------|-------------------|-------------------|-----------|--------|-----|

1 个答案:

答案 0 :(得分:1)

这是您要寻找的东西吗?

 select workers.id, full_name,DATEDIFF(second, min(start_time), max(end_time)) / 3600.0
    from workers inner join shcedule
    on workers.id=shcedule.worker_id
    group by workers.id,full_name