|
发表于 2011-10-31 13:05:28
|
显示全部楼层
Re:!如何查询兩笔数据的时间差
SQL code
with tbl as
(
select 'a' as proc, sysdate - 0.8 as deal_time from dual
union all
select 'a' as proc, sysdate - 0.5 as deal_time from dual
union all
select 'a' as proc, sysdate - 0.2 as deal_time from dual
union all
select 'b' as proc, sysdate - 0.5 as deal_time from dual
union all
select 'b' as proc, sysdate as deal_time from dual
)
select t.proc, t.deal_time_dif
from (select proc,
nvl(lead(deal_time) over(partition by proc order by deal_time), deal_time) - deal_time as deal_time_dif,
iasc
from (select proc, deal_time,
row_number() over(partition by proc order by deal_time) as iasc,
row_number() over(partition by proc order by deal_time desc) as idesc
from tbl)
where iasc = 1 or idesc = 1) t
where iasc = 1; |
|