Oracle SQL-多个ID的两个日期之间的差异

Oracle SQL-多个ID的两个日期之间的差异,sql,oracle,date-difference,Sql,Oracle,Date Difference,这就是我的表/输出的外观: ID ID_2 Status Timestamp ------- ----------- ------------------- -------------------- 4613840 19668170 Submitted 05-06-2015 16:37:00 4613840 19668330 Submitted 05-06-2015 16:44:00 4

这就是我的表/输出的外观:

ID       ID_2        Status              Timestamp
-------  ----------- ------------------- --------------------
4613840  19668170    Submitted           05-06-2015 16:37:00
4613840  19668330    Submitted           05-06-2015 16:44:00
4613840  19668409    In Progress         05-06-2015 16:48:00
4613840  19669674    SupplierPend        05-06-2015 17:43:00
4613840  19705863    SupplierPend        09-06-2015 15:01:00
4613840  19735270    In Progress         12-06-2015 11:38:00
4613840  19735282    Information Pend    12-06-2015 11:38:00
4613840  19735383    Closed              12-06-2015 11:42:00
我需要在末尾添加另一列,其中包含日期之间的差异(以分钟为单位)

预期产出:

ID       ID_2        Status              Timestamp            Result
-------  ----------- ------------------- -------------------  ---------
4613840  19668170    Submitted          05-06-2015 16:37:00   0:07:00
4613840  19668330    Submitted          05-06-2015 16:44:00   0:04:00
4613840  19668409    In Progress        05-06-2015 16:48:00   0:55:00
4613840  19669674    SupplierPend       05-06-2015 17:43:00   93:18:00
4613840  19705863    SupplierPend       09-06-2015 15:01:00   68:37:00
4613840  19735270    In Progress        12-06-2015 11:38:00   0:00:00
4613840  19735282    Information Pend   12-06-2015 11:38:00   0:04:00
4613840  19735383    Closed             12-06-2015 11:42:00

第一行的结果是ID为19668330的时间戳与ID为19668170的时间戳之间的差异。如果您希望下一行的时间以分钟为单位(按时间顺序),则使用
lead()
。以下是一天的分数差:

select t.*,
       (lead(timestamp) over (partition by id order by timestamp) - timestamp
       ) as DayFrac
from table t;
这会将值转换为分钟:

select t.*,
       (lead(timestamp) over (partition by id order by timestamp) - timestamp
       ) * 60 * 24 as Minutes
from table t;
尝试以下方法:

with x as (
select 1 as id, 'Start' as status, to_timestamp('20150721 07:15:00', 'YYYYMMDD HH24:MI:SS') as ts from dual
union all
select 1 as id, 'Processing' as status, to_timestamp('20150721 08:00:00', 'YYYYMMDD HH24:MI:SS') as ts from dual
union all
select 1 as id, 'Completed' as status, to_timestamp('20150721 08:05:00', 'YYYYMMDD HH24:MI:SS') as ts from dual
union all
select 2 as id, 'Start' as status, to_timestamp('20150721 08:15:00', 'YYYYMMDD HH24:MI:SS') as ts from dual
union all
select 2 as id, 'Processing' as status, to_timestamp('20150721 08:30:00', 'YYYYMMDD HH24:MI:SS') as ts from dual
union all
select 2 as id, 'Completed' as status, to_timestamp('20150721 08:32:00', 'YYYYMMDD HH24:MI:SS') as ts from dual
)
select id, status, ts1, ts2 - ts1 as timespan
from (
  select id, status, ts as ts1, lead(ts, 1) over (partition by id order by ts asc) ts2
  from x
)
输出:

ID  STATUS  TS1 TIMESPAN
1   Start   7/21/2015 7:15:00.000000000 AM  +00 00:45:00.000000
1   Processing  7/21/2015 8:00:00.000000000 AM  +00 00:05:00.000000
1   Completed   7/21/2015 8:05:00.000000000 AM  
2   Start   7/21/2015 8:15:00.000000000 AM  +00 00:15:00.000000
2   Processing  7/21/2015 8:30:00.000000000 AM  +00 00:02:00.000000
2   Completed   7/21/2015 8:32:00.000000000 AM  

在什么日期之间?请编辑您的问题并提供您想要的结果。这些在Oracle中定义为日期或时间戳吗?非常有效!好极了,伙计!