select a.time-b.time from 
(select id,time from test where id=(select max(id) from test) ) a,
(select id,time from test where id=(select max(id)-1 from test)) b;

解决方案 »

  1.   

    用分析函数
    SELECT t.id,t.time,
           lead(t.time,1,NULL) over (ORDER BY t.time) Next_time
                  FROM table t
      

  2.   

    以time作为排序可能不能实现楼主意,有可能最后记录time会比前一个小SQL> create table aaa(id varchar2(2),time date);Table createdSQL> insert into aaa values('1',to_date('2003-1-1','yyyy-mm-dd'));1 row insertedSQL> insert into aaa values('1',to_date('2003-2-6','yyyy-mm-dd'));1 row insertedSQL> insert into aaa values('1',to_date('2003-1-5','yyyy-mm-dd'));1 row insertedSQL> select * from aaa;ID TIME
    -- -----------
    1  2003-1-1
    1  2003-2-6
    1  2003-1-5SQL> select time-lead(time,1,null) over(order by time) from aaa;TIME-LEAD(TIME,1,NULL)OVER(ORD
    ------------------------------
                                -4
                               -32上述就不能以order by time,这样吧:
    SQL> select time-lead(time,1,null) over(order by rownum) from aaa;TIME-LEAD(TIME,1,NULL)OVER(ORD
    ------------------------------
                               -36
                                32
      

  3.   

    我对于以上问题明白了大家得意思
    如果问题是这样的
    tablename:test
    字段:id,id2,time
    记录内容:
    id   id2 time 
    1    11  2003-2-1
    2    22  2003-2-3
    3    22  2003-2-12
    4    11  2003-3-1
    5    22  2003-3-12
    要求求出id2=11最后2条纪录的时间差
      

  4.   

    大概可以如下,我没有正式运行
    select a.time -b.time from (
    select rownum,id2,time form 
    (select id,time from test where id2=11 order by time desc)
    where rownum<3 ) a, select rownum,time form 
    (select id,id2 time from test where id2=11 order by time desc)
    where rownum<3 ) b
    where a.rownum=2 and b.rownum=1 and a.id2=b.id2
      

  5.   

    rownum<3 是什么意思??
      

  6.   

    select time-lead(time,1,null) over(partition by id2 order by rownum) from table_name where id2='11';
      

  7.   

    你在分析函数的语句上加上where条件不就可以了吗?
      

  8.   

    运行时报错rownum 是无效的列名
      

  9.   

    如果问题是这样的
    tablename:test
    字段:id,id2,time
    记录内容:
    id   id2 time 
    1    11  2003-2-1
    2    22  2003-2-3
    3    22  2003-2-12
    4    11  2003-3-1
    5    22  2003-3-12
    要求求出id2=11最后2条纪录的时间差
    也就是求第4条纪录和第1条纪录的时间差
    我不懂分析函数,能不能写的具体点!
    谢谢
      

  10.   

    我把题目说得更清楚了
    如果问题是这样的
    tablename:test
    字段:id,id2,time
    记录内容:
    id   id2 time 
    1    11  2003-2-1
    2    22  2003-2-3
    3    22  2003-2-12
    4    11  2003-3-1
    5    22  2003-3-12
    6    11  2003-4-1
    要求求出id2=22最后2条纪录的时间差
    也就是求第5条纪录和第3条纪录的时间差
    我只想得到一条结果
    我不懂分析函数,能不能写的具体点!
    谢谢
      

  11.   

    把table_name换成test!
    select time-lead(time,1,null) over(partition by id2 order by rownum) from test
     where id2='11';
      

  12.   

    select last_value(time_minus) over() from (select rownum rm,time-lead(time,1,null) over(partition by id2 order by rownum) time_minus from test where id2='11') where rm=1;
      

  13.   

    radish由于分数不多所以没给你
    一又有机会给你补上