简体   繁体   English

选择两行之间的差异

[英]select difference between two rows

I have select the return to me: 我选择了退货给我:

NAME                            REBUILD
------------------------------  ---------
IDXD_INVOICE_LINE               25-01-14 17:00:58
IDXD_INST                       25-01-14 17:08:08
IDXD_IMPORT                     25-01-14 17:08:39
IDXD_IMPORT_FILE                25-01-14 18:08:02
IDXD_HP                         25-01-14 18:08:37
IDXD_TASK                       25-01-14 18:09:08
IDXD_RULE                       25-01-14 18:09:46
IDXD_USER                       26-01-14 03:48:57

I now want to get the difference between a row and the row above it, the first row will difference with 25-01-14 03:00:00 . 现在,我想获得一行与一行以上的区别,第一行与25-01-14 03:00:00 Result will be: 结果将是:

NAME                            REBUILD
------------------------------  ---------
IDXD_INVOICE_LINE               02:00:58
IDXD_INST                       00:08:08
IDXD_IMPORT                     00:00:31
IDXD_IMPORT_FILE                00:59:23
IDXD_HP                         00:00:35
IDXD_TASK                       00:00:31
IDXD_RULE                       00:00:38
IDXD_USER                       09:40:11

I assume the "row above" is based on the timestamp. 我认为“行上方”是基于时间戳的。

select name,
       to_char(trunc(sysdate) + (rebuild - prev_rebuild) / (24*60*60)), 'HH24:MI:SS')
from (select t.*, 
             lag(rebuild) over (order by rebuild) as prev_rebuild
      from t
     ) t;

This uses a trick to get the time output in the format you want it. 这使用技巧来获取所需格式的时间输出。 It works when the difference is less than 24 hours. 当差异少于24小时时,它将起作用。

EDIT: 编辑:

If it can be longer than 24 hours, then you need to put the expression together yourself: 如果可能超过24小时,那么您需要自己将表达式放在一起:

select name,
       (to_char(trunc((rebuild - prev_rebuild) * 24), '00') || ':' ||
        to_char(mod(trunc((rebuild - prev_rebuild)*24*60), 60), '00') || ':' ||
        to_char(mod(trunc((rebuild - prev_rebuild)*24*60*60), 60), '00')
       ) as timestamp
from (select t.*, 
             lag(rebuild) over (order by rebuild) as prev_rebuild
      from t
     ) t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM