2017-08-03 93 views
1

你能幫我換班嗎?我需要爲每個事件分配它是什麼。 我想統計在每個班次中機器完成多少項目。 「ILE」的意思是「有多少項目移位共做」甲骨文輪班工作日期

SELECT lz.EVENT, 
    TO_CHAR(lz.DATA_ZATW, 'YYYY-MM-DD') AS DATA, 
      CASE WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI') BETWEEN '06:00' AND '13:59' THEN 'First Shift' 
       WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI') BETWEEN '14:00' AND '21:59' THEN 'Second Shift' 
       WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI') BETWEEN '22:00' AND '23:59' THEN 'Third Shift' 
       WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI') BETWEEN '00:00' AND '05:59' THEN 'Third Shift' 
       END AS SHIFT 

FROM PLC.LIST lz; 

但是當我現在檢查查詢和我有2017年8月3日第三次轉變,這是錯誤的,因爲根據SYSDATE我shound必須先轉變只要。 有什麼想法?

編輯:

整個查詢

SELECT "DATA", 
      "ZMIANA", 
      "SOV_GAO_1_ILE", 
      "SOV_GAD_2_ILE", 
      "SOV_GAM_3_ILE" 
     FROM (SELECT TO_CHAR(lz.DATA_ZATW, 'YYYY-MM-DD') AS DATA, 
        CASE WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI:SS') BETWEEN '06:00:00' AND '13:59:59.999999' THEN 1 
         WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI:SS') BETWEEN '14:00:00' AND '21:59:59.999999' THEN 2 
         WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI:SS') BETWEEN '22:00:00' AND '23:59:59.999999' THEN 3 
         WHEN TO_CHAR(lz.DATA_ZATW, 'HH24:MI:SS') BETWEEN '00:00:00' AND '05:59:59.999999' THEN 3 
         END AS ZMIANA, 
        lz.LINIA, 
        lz.DETAL 
      FROM PLC.LISTA_ZDARZEN lz 
      GROUP BY lz.DATA_ZATW, 
        lz.LINIA, 
        lz.DETAL) PIVOT (COUNT(DETAL) AS ILE FOR LINIA IN ('SOV_GAO_1' SOV_GAO_1, 'SOV_GAD_2' SOV_GAD_2, 'SOV_GAM_3' SOV_GAM_3)); 

編輯:

轉變

6-14 01.08 first shift 
14-22 01.08 second shift 
22-00 01.08 third shift 
00-6 02.08 third shift 

6-14 02.08 first shift 
14-22 02.08 second shift 
22-00 02.08 third shift 
00-6 03.08 third shift 

6-14 03.08 first shift 
14-22 03.08 second shift 
22-00 03.08 third shift 
00-6 04.08 third shift 

這裏時間:

SELECT TO_DATE('2017-08-01 23:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 01.08 Third shift 
    SELECT TO_DATE('2017-08-01 23:57:58', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 01.08 Third shift 
    SELECT TO_DATE('2017-08-02 03:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 01.08 Third shift 
    SELECT TO_DATE('2017-08-02 04:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 01.08 Third shift 
    SELECT TO_DATE('2017-08-02 06:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 First shift 
    SELECT TO_DATE('2017-08-02 07:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 First shift 
    SELECT TO_DATE('2017-08-02 10:24:34', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 First shift 
    SELECT TO_DATE('2017-08-02 14:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Second shift 
    SELECT TO_DATE('2017-08-02 15:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Second shift 
    SELECT TO_DATE('2017-08-02 19:21:10', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Second shift 
    SELECT TO_DATE('2017-08-02 22:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Third shift 
    SELECT TO_DATE('2017-08-02 23:55:20', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Third shift 
    SELECT TO_DATE('2017-08-02 23:55:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Third shift 
    SELECT TO_DATE('2017-08-03 00:02:34', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Third shift 
    SELECT TO_DATE('2017-08-03 00:05:34', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Third shift 
    SELECT TO_DATE('2017-08-03 00:10:38', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 02.08 Third shift 
    SELECT TO_DATE('2017-08-03 09:01:24', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 03.08 First shift 
    SELECT TO_DATE('2017-08-03 09:01:32', 'yyyy-mm-dd hh24:mi:ss') FROM DUAL UNION ALL -- 03.08 First shift 
    SELECT TO_DATE('2017-08-03 17:01:10', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 03.08 Second shift 
    SELECT TO_DATE('2017-08-03 19:28:45', 'yyyy-mm-dd hh24:mi:ss') FROM DUAL UNION ALL -- 03.08 Second shift 
    SELECT TO_DATE('2017-08-03 23:54:56', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 03.08 Third shift 
    SELECT TO_DATE('2017-08-03 23:55:20', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL -- 03.08 Third shift 
    SELECT TO_DATE('2017-08-03 23:55:56', 'yyyy-mm-dd hh24:mi:ss') FROM DUAL   -- 03.08 Third shift 
+0

請提供一些示例數據和預期的結果。 – APC

+0

嘗試'TO_CHAR(lz.DATA_ZATW,'YYYY-MM-DD HH24:MI:SS')AS DATA'以驗證您的CASE表達式。 –

+0

我只想計算每個班次機器完成多少項目。 –

回答

0

也許試試這個:

CASE WHEN TO_CHAR(lz.DATA_ZATW, 'HH24MI') BETWEEN 600 AND 1359 THEN 'First Shift' 
    WHEN TO_CHAR(lz.DATA_ZATW, 'HH24MI') BETWEEN 1400 AND 2159 THEN 'Second Shift' 
    WHEN TO_CHAR(lz.DATA_ZATW, 'HH24MI') BETWEEN 2200 AND 2359 THEN 'Third Shift' 
    WHEN TO_CHAR(lz.DATA_ZATW, 'HH24MI') BETWEEN 0 AND 559 THEN 'Third Shift' 
END AS SHIFT 

或像這樣:

CASE WHEN (lz.DATA_ZATW - TRUNC(lz.DATA_ZATW)) * INTERVAL '1' DAY BETWEEN INTERVAL '6' HOUR AND INTERVAL '13:59:59.999999' HOUR TO SECOND THEN 'First Shift' 
CASE WHEN (lz.DATA_ZATW - TRUNC(lz.DATA_ZATW)) * INTERVAL '1' DAY BETWEEN INTERVAL '14' HOUR AND INTERVAL '21:59:59.999999' HOUR TO SECOND THEN 'Second Shift' 
... 

根據您的樣本數據的工作原理:

WITH t (DATA_ZATW) AS (
    SELECT TO_DATE('2017-08-03 09:03:31', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:03:11', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:03:07', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:02:44', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:02:22', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:02:21', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:01:58', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:01:32', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:01:24', 'yyyy-mm-dd hh24:mi:ss') FROM dual) 
SELECT DATA_ZATW, 
    CASE 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 600 AND 1359 THEN 'First Shift' 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 1400 AND 2159 THEN 'Second Shift' 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 2200 AND 2359 THEN 'Third Shift' 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 0 AND 559 THEN 'Third Shift' 
    END AS SHIFT 
FROM t; 

DATA_ZATW SHIFT 
03.08.2017 09:03:31 First Shift 
03.08.2017 09:03:11 First Shift 
03.08.2017 09:03:07 First Shift 
03.08.2017 09:02:44 First Shift 
03.08.2017 09:02:22 First Shift 
03.08.2017 09:02:21 First Shift 
03.08.2017 09:01:58 First Shift 
03.08.2017 09:01:32 First Shift 
03.08.2017 09:01:24 First Shift 

我假定你正在尋找的東西像結束這個:

WITH t (DATA_ZATW) AS (
    SELECT TO_DATE('2017-08-03 09:03:31', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:03:11', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:03:07', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:02:44', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:02:22', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:02:21', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:01:58', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:01:32', 'yyyy-mm-dd hh24:mi:ss') FROM dual UNION ALL 
    SELECT TO_DATE('2017-08-03 09:01:24', 'yyyy-mm-dd hh24:mi:ss') FROM dual) 
SELECT TRUNC(DATA_ZATW), SHIFT, COUNT(*) 
FROM 
(
SELECT DATA_ZATW, 
    CASE 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 600 AND 1359 THEN 'First Shift' 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 1400 AND 2159 THEN 'Second Shift' 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 2200 AND 2359 THEN 'Third Shift' 
     WHEN TO_CHAR(DATA_ZATW, 'HH24MI') BETWEEN 0 AND 559 THEN 'Third Shift' 
    END AS SHIFT 
FROM t 
) 
GROUP BY TRUNC(DATA_ZATW), SHIFT; 
+0

它無法正常工作。我今天還是第三班: '2017年8月2日\t第一換擋 2017年8月2日\t第二移位 2017年8月2日\t第三班 2017年8月3日\t第一換擋 2017-08-03 \t第三班' –

+1

什麼是時間值?只提供一天對於這個查詢來說是無用的。 –

+0

我編輯後 - >看看:) –

0

我認爲你唯一需要做的就是從data_zatw減去6小時,把它分配到前一天。

select data_zatw, trunc(data_zatw - interval '6' hour) shift_date, 
     case when to_char(data_zatw, 'HH24') >= '06' 
      and to_char(data_zatw, 'HH24') < '14' then 1 
      when to_char(data_zatw, 'HH24') >= '14' 
      and to_char(data_zatw, 'HH24') < '22' then 2 
      when to_char(data_zatw, 'HH24') >= '22' 
       or to_char(data_zatw, 'HH24') < '06' then 3 
     end shift 
    from lz 

然後讓你的支點和聚合和樞紐。

測試數據:

with lz as (
    select to_date(column_value, 'yyyy-mm-dd hh24:mi:ss') data_zatw 
    from table(
     sys.odcivarchar2list(
     '2017-08-01 23:54:56', '2017-08-01 23:57:58', '2017-08-02 03:54:56', 
     '2017-08-02 04:54:56', '2017-08-02 06:54:56', '2017-08-02 07:54:56', 
     '2017-08-02 10:24:34', '2017-08-02 14:54:56', '2017-08-02 15:54:56', 
     '2017-08-02 19:21:10', '2017-08-02 22:54:56', '2017-08-02 23:55:20', 
     '2017-08-02 23:55:56', '2017-08-03 00:02:34', '2017-08-03 00:05:34', 
     '2017-08-03 00:10:38', '2017-08-03 09:01:24', '2017-08-03 09:01:32', 
     '2017-08-03 17:01:10', '2017-08-03 19:28:45', '2017-08-03 23:54:56', 
     '2017-08-03 23:55:20', '2017-08-03 23:55:56'))) 
select data_zatw, trunc(data_zatw - interval '6' hour) shift_date, 
     case when to_char(data_zatw, 'HH24') >= '06' 
      and to_char(data_zatw, 'HH24') < '14' then 1 
      when to_char(data_zatw, 'HH24') >= '14' 
      and to_char(data_zatw, 'HH24') < '22' then 2 
      when to_char(data_zatw, 'HH24') >= '22' 
       or to_char(data_zatw, 'HH24') < '06' then 3 
     end shift 
    from lz