具有逗号分隔标识符的同一列上的时间戳(第2行和第1行)和(第3行和第2行)的差异

时间:2017-12-14 11:00:05

标签: sql oracle

我有一张表格如下:

Eventid                      action  callstatus datetime                  
----------------------       ------  ---------- --------                  
948687,948689,948690,948691, GR      NEW       14-DEC-17 05.10.07.000000000 AM
948690                       UR      WIP       14-DEC-17 05.10.22.000000000 AM      
948690                       UR      DONE      14-DEC-17 05.10.23.000000000 AM

需要获取datetime列的Row2和Row1之间的时差以及datetime列的Row3和Row2之间的差异。

我需要为多个eventid做这个输入。

请协助。

2 个答案:

答案 0 :(得分:0)

Oracle解决方案。您必须将eventid转换为单独的行。有很多方法,我在这里使用" xmltable"方法。然后使用lag计算差异。

with t(eventid, action, callstatus, datetime) as (
    select '948687,948689,948690,948691,', 'GR', 'NEW', 
                                   timestamp '2017-12-14 05:10:07' from dual union all
    select '948690', 'UR', 'WIP',  timestamp '2017-12-14 05:10:22' from dual union all
    select '948690', 'UR', 'DONE', timestamp '2017-12-14 05:10:23' from dual ),
-- end of simulated data, query starts here:
cte as (
    select t.*, '"'||replace(t.eventid, ',', '","')||'"' evid from t)
select trim(column_value) eid, eventid, action, callstatus, datetime,
       datetime - lag(datetime) over (partition by trim(column_value) order by datetime) diff
  from cte, xmltable(evid)
  order by 1, datetime

输出:

EID     EVENTID                      ACTION CALLSTATUS DATETIME          DIFF
------- ---------------------------- ------ ---------- ----------------- -------------------
948687  948687,948689,948690,948691, GR     NEW        17/12/14 05:10:07
948689  948687,948689,948690,948691, GR     NEW        17/12/14 05:10:07
948690  948687,948689,948690,948691, GR     NEW        17/12/14 05:10:07
948690  948690                       UR     WIP        17/12/14 05:10:22 +000000000 00:00:15
948690  948690                       UR     DONE       17/12/14 05:10:23 +000000000 00:00:01
948691  948687,948689,948690,948691, GR     NEW        17/12/14 05:10:07

答案 1 :(得分:0)

这是Oracle DB。我正在使用这样的SQL。但是想要为多个事件ID调整一点点并适当地输出时差

有什么建议吗?

从AWS2.audit_comm_data中选择eventid,datetime - lead(datetime)over(order by datetime),其中eventid喜欢'%948643%'或者甚至喜欢'%948690%';