Oracle SQL LAG 函数return 时间计算错误

Oracle SQL LAG function return the wrong calculation in Time

我在第一行第 Different_Time 列的计算是不正确的数据,第二行是正确的数据,直到第 10 行再次是不正确的数据,然后下一行计算返回如果有人可以帮助我解决这个问题,请使用 LAG function.Appreciate 再次正确计算。谢谢。 [![这是数据样本][1]][1]

这是我的查询如下:

select EQP_ID,LOT_ID,ACTIVITY,DATE_TIME
TO_CHAR (DATE '1900-01-01' + ABS ( LAG(DATE_TIME,1)OVER(PARTITION BY EQP_ID ORDER BY DATE_TIME) - DATE_TIME), 'HH24:MI:SS')AS Different_Time
from STG_RMSTMP_CJ.ota_activity where EQP_ID = '93K-P007'

这是输出结果: [1]: https://i.stack.imgur.com/mjhb7.png

EQP_ID     LOT_ID      ACTIVITY         DATE_TIME             Different_Time 

93K-P007   GC00495     LOAD_LOT     2020-08-03 16:48:48           NULL
93K-P007   GC00495     LOAD_LOT     2020-08-05 11:51:44         19:02:56
93K-P007   GC00495     LOAD_LOT     2020-08-05 11:57:24         00:05:40
93K-P007   GC00495     LOAD_LOT     2020-08-05 15:14:21         03:16:57
93K-P007   WD72120     LOAD_LOT     2020-08-05 15:17:17         00:02:56
93K-P007   GC00495     LOAD_LOT     2020-08-05 15:28:43         00:11:26
93K-P007   GC00495     LOAD_LOT     2020-08-05 15:30:00         00:01:17
93K-P007   GC00495     LOAD_LOT     2020-08-05 15:42:44         00:12:44
93K-P007   GC00495     LOAD_LOT     2020-08-07 10:28:18         18:45:34
93K-P007   GC00495     LOAD_LOT     2020-08-07 10:33:31         00:05:13

你的第一个逻辑没问题。您还应该考虑日差。但仅出于为您提供所需解决方案的唯一目的,请尝试以下。

Solution

WITH MAIN
     AS (SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-03 16:48:48', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-05 11:51:44', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-05 11:57:24', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-05 15:14:21', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'WD72120' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-05 15:17:17', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-05 15:28:43', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-05 15:30:00', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-05 15:42:44', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-07 10:28:18', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL
         UNION ALL
         SELECT '93K-P007' AS EQP_ID,
                'GC00495' LOT_ID,
                'LOAD_LOT' ACTIVITY,
                TO_DATE ('2020-08-07 10:33:31', 'YYYY-MM-DD HH24:MI:SS')
                   AS DATE_TIME
           FROM DUAL)
SELECT EQP_ID,
               LOT_ID,
               ACTIVITY,
               DATE_TIME,
               TO_CHAR(
               LPAD(ABS(EXTRACT(HOUR FROM TO_TIMESTAMP(TO_CHAR(LAG (DATE_TIME, 1 ,DATE_TIME)OVER (PARTITION BY EQP_ID ORDER BY DATE_TIME),'yyyy-mm-dd HH24:MI:SS'),'yyyy-mm-dd HH24:MI:SS')) -
               EXTRACT(HOUR FROM TO_TIMESTAMP(TO_CHAR(DATE_TIME,'yyyy-mm-dd HH24:MI:SS'),'yyyy-mm-dd HH24:MI:SS'))) ,2,'0')||':'||
               LPAD(ABS(EXTRACT(MINUTE FROM TO_TIMESTAMP(TO_CHAR(LAG (DATE_TIME, 1 ,DATE_TIME)OVER (PARTITION BY EQP_ID ORDER BY DATE_TIME),'yyyy-mm-dd HH24:MI:SS'),'yyyy-mm-dd HH24:MI:SS')) -
               EXTRACT(MINUTE FROM TO_TIMESTAMP(TO_CHAR(DATE_TIME,'yyyy-mm-dd HH24:MI:SS'),'yyyy-mm-dd HH24:MI:SS'))) ,2,'0')||':'||
               LPAD(ABS(EXTRACT(SECOND FROM TO_TIMESTAMP(TO_CHAR(LAG (DATE_TIME, 1 ,DATE_TIME)OVER (PARTITION BY EQP_ID ORDER BY DATE_TIME),'yyyy-mm-dd HH24:MI:SS'),'yyyy-mm-dd HH24:MI:SS')) -
               EXTRACT(SECOND FROM TO_TIMESTAMP(TO_CHAR(DATE_TIME,'yyyy-mm-dd HH24:MI:SS'),'yyyy-mm-dd HH24:MI:SS'))),2,'0') 
               ) AS DIFF
          FROM MAIN
         WHERE EQP_ID = '93K-P007'

您的查询很好,但您只需要格式化处理天、小时...

我已经为你做了同样的事情:

Demo

select EQP_ID,LOT_ID,ACTIVITY,DATE_TIME, abs(extract (DAY from Different_Time))||' '||abs(extract (HOUR from Different_Time))||':'||abs(extract (MINUTE from Different_Time))||':'||abs(extract (SECOND from Different_Time)) AS Different_Time from (
select EQP_ID,LOT_ID,ACTIVITY,DATE_TIME,
 LAG(DATE_TIME,1) OVER(PARTITION BY EQP_ID ORDER BY DATE_TIME) - DATE_TIME AS Different_Time
from table1 where EQP_ID = '93K-P007');