【问题标题】:Finding a record, the last record with the same id_hash, and recording the time difference of the two records in the column找到一条记录,最后一条id_hash相同的记录,并在列中记录两条记录的时间差
【发布时间】:2020-07-07 06:53:50
【问题描述】:

我有一个关于 oracle 的查询,我将在其中查询表。我想重新处理这个请求,我有一个列状态。状态中有2个事件:START和Stop,我还有一个hash列,这个任务的key正在执行。这个哈希可以重复

select id, status, date_cr,hesh from SEC_ROUTE
我的答案:

id   status           date_cr                hesh
9    STOP             26.03.2020 14:45:41    7A8E93CA359A39133B6AAC93123    
8    START            26.03.2020 12:23:47    7A8E93CA359A39133B6AAC93127
7    STOP             26.03.2020 11:12:41    7A8E93CA359A39133B6AAC93111  
6    START            26.03.2020 10:56:40    7A8E93CA359A39133B6AAC93111
5    STOP             26.03.2020 10:23:41    7A8E93CA359A39133B6AAC93B09
4    START            26.03.2020 08:13:40    7A8E93CA359A39133B6AAC93B09
3    START            26.03.2020 07:23:41    7A8E93CA359A39133B6AAC93123
2    STOP             26.03.2020 06:18:56    7A8E93CA359A39133B6AAC93123
1    START            26.03.2020 04:12:23    7A8E93CA359A39133B6AAC93123

我要STATUS栏,这是stop的状态,用来记录工作开始和结束的区别 示例(记录差异的格式不是基本的):

    id  status             date_cr                hesh
    9   STOP(02:21:54)     26.03.2020 14:45:41    7A8E93CA359A39133B6AAC93123 
    8   START              26.03.2020 12:23:47    7A8E93CA359A39133B6AAC93127
    7   STOP(00:16:01)     26.03.2020 11:12:41    7A8E93CA359A39133B6AAC93111
    6   START              26.03.2020 10:56:40    7A8E93CA359A39133B6AAC93111
    5   STOP(02:10:01)     26.03.2020 10:23:41    7A8E93CA359A39133B6AAC93B09
    4   START              26.03.2020 08:13:40    7A8E93CA359A39133B6AAC93B09
    3   START              26.03.2020 07:23:41    7A8E93CA359A39133B6AAC93123
    2   STOP(02:06:33)     26.03.2020 06:18:56    7A8E93CA359A39133B6AAC93123
    1   START              26.03.2020 04:12:23    7A8E93CA359A39133B6AAC93123
0

我在可编程数据库方面还没有太多经验,所以我什至不知道是否可以做到。如果有人愿意提供帮助,我将不胜感激)

【问题讨论】:

    标签: sql oracle oracle11g oracle-apex


    【解决方案1】:

    假设您不能连续有两个 STOP 并且您想要最后一个 START 值,请使用 lag 查找最后一个开始时间。并从当前时间减去:

    create table t (
      id int, status varchar2(5), date_cr timestamp, hash varchar2(32)
    );
    insert into t values (1, 'STOP', to_date ( '26.03.2020 14:45:41', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93123' );    
    insert into t values (2, 'START', to_date ( '26.03.2020 12:23:47', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93127' );
    insert into t values (3, 'STOP', to_date ( '26.03.2020 11:12:41', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93111' );  
    insert into t values (4, 'START', to_date ( '26.03.2020 10:56:40', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93111' );
    insert into t values (5, 'STOP', to_date ( '26.03.2020 10:23:41', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93B09' );
    insert into t values (6, 'START', to_date ( '26.03.2020 08:13:40', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93B09' );
    insert into t values (7, 'START', to_date ( '26.03.2020 07:23:41', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93123' );
    insert into t values (8, 'STOP', to_date ( '26.03.2020 06:18:56', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93123' );
    insert into t values (9, 'START', to_date ( '26.03.2020 04:12:23', 'dd.mm.yyyy hh24:mi:ss' ), '7A8E93CA359A39133B6AAC93123' );
    commit;
    
    select id,
           status,
           date_cr,
           case 
             when status = 'STOP' then
               date_cr - lag ( date_cr ) over (
                 order by date_cr 
               )
           end tm
    from   t
    order  by date_cr desc;
    
    ID    STATUS    DATE_CR                           TM                    
        1 STOP      26-MAR-2020 14.45.41.000000000    +00 02:21:54.000000    
        2 START     26-MAR-2020 12.23.47.000000000    <null>                 
        3 STOP      26-MAR-2020 11.12.41.000000000    +00 00:16:01.000000    
        4 START     26-MAR-2020 10.56.40.000000000    <null>                 
        5 STOP      26-MAR-2020 10.23.41.000000000    +00 02:10:01.000000    
        6 START     26-MAR-2020 08.13.40.000000000    <null>                 
        7 START     26-MAR-2020 07.23.41.000000000    <null>                 
        8 STOP      26-MAR-2020 06.18.56.000000000    +00 02:06:33.000000    
        9 START     26-MAR-2020 04.12.23.000000000    <null> 
    

    【讨论】:

    • 谢谢,这太酷了,我什至不认为这个要求是可能的。但是2次STOP是可以的,因为它可以包含两条不同的路由,有可能2次会停止,也有可能开始,需要注意路由hash
    • 我们可以在这样一个 taco 变体下编辑这个查询,同时注意哈希,这也很重要,我会感谢你的帮助))
    • 你能帮我吗? ,然后我没有找到我的问题的答案(((
    • 我不确定你所说的 could edit this query under such a taco variant and also pay attention to the hash 是什么意思 - 你能澄清一下吗?
    • 另外,当连续有两个STOP时,输出应该是什么?请更新示例以包含此
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-31
    • 2017-05-08
    • 2019-12-28
    • 1970-01-01
    相关资源
    最近更新 更多