Oracle改变了日期

时间:2017-08-03 05:53:59

标签: oracle

你能帮助我换班吗?我需要为每个事件分配什么样的转变。 我想要计算每个班次中机器完成的物品数量。 " 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-08-03的第三班并且它是错误的因为根据sysdate我只有第一次转移。 有什么想法吗?

编辑:

整个查询

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

2 个答案:

答案 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;

答案 1 :(得分: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