日期:2014-05-17 浏览次数:21509 次
select t1.id,t1.time,t2.time,t2.time-t1.time val from (select rownum rn,id,time from test) t1, (select rownum rn,id,time from test) t2 where t1.rn=t2.rn-1 and mod(t1.rn,2)=1;
------解决方案--------------------
应该可以用行数配合lag实现吧
------解决方案--------------------
就是跟数据编个序号,用相邻号求差值 select t1.id,t1.time,t2.time,t2.time-t1.time val from (select rownum rn1,id,time from test) t1 inner join (select rownum rn2,id,time from test) t2 on t1.rn1=t2.rn2-1 ;
------解决方案--------------------
create table test(
id int primary key,
time date
);
insert into test values(1,to_date('2012.7.23','yyyy.mm.dd'));
insert into test values(3,to_date('2012.7.24','yyyy.mm.dd'));
insert into test values(5,to_date('2012.7.30','yyyy.mm.dd'));
insert into test values(7,to_date('2012.8.1','yyyy.mm.dd'));
insert into test values(8,to_date('2012.8.23','yyyy.mm.dd'));
insert into test values(9,to_date('2012.8.24','yyyy.mm.dd'));
insert into test values(11,to_date('2012.8.22','yyyy.mm.dd'));
select id,time,time-pre_time from (
select id,time,lag(time) over (order by id) pre_time from test
)
------解决方案--------------------
这样应该满足你的需求
with t as ( select 1 id,date'2012-01-29' tdate from dual union all select 3,date'2012-02-03' from dual union all select 2,date'2012-01-28' from dual union all select 56,date'2012-02-09' from dual union all select 7,date'2012-02-05' from dual ) select rn,id,tdate, decode(mod(rn,2),0 ,tdate-lag(tdate) over(order by rn)) from (select rownum rn,id,tdate from ( select id,tdate from t order by id))t1