+ -
当前位置:首页 → 问答吧 → Oracle 跪求时间倒序获取第二条记录

Oracle 跪求时间倒序获取第二条记录

时间:2011-12-01

来源:互联网

数据如下:
REEMPLOYEEID SCANTIME 
100000000088 2011-12-01 10:27:14 
100000000088 2011-12-01 10:32:00 
100000000088 2011-12-01 10:32:06 
100000000104 2011-12-01 10:35:54 
100000000088 2011-12-01 10:55:44 
100000000106 2011-12-01 10:56:33 



求结果:
REEMPLOYEEID SCANTIME 

100000000088 2011-12-01 10:55:44 

作者: lovezx1028   发布时间: 2011-12-01

SQL code
select sub.reemployeeid, sub.scantime
from
(with a as(
select '100000000088' as reemployeeid, '2011-12-01 10:27:14' as scantime from dual
union all
select '100000000088','2011-12-01 10:32:00' from dual
union all
select '100000000088','2011-12-01 10:32:06' from dual
union all
select '100000000104','2011-12-01 10:35:54' from dual
union all
select '100000000088','2011-12-01 10:55:44' from dual
union all
select '100000000106','2011-12-01 10:56:33' from dual
)
select reemployeeid, scantime, row_number() 
over (order by scantime desc) as ss from a) sub
where sub.ss = 2

作者: yixilan   发布时间: 2011-12-01

提供你一个方法供参考
select * from (select * from ss ORDER BY CANTIME desc )
where rownum<=2
minus 
select * from (select * from ss ORDER BY CANTIME desc )
where rownum<=1

作者: money729   发布时间: 2011-12-01

SQL code


select reemployeeid, scantime
  from (select t.reemployeeid,
               t.scantime,
               RANK() OVER(order by scantime) as RowNumber
          from tab t) b
 where b.RowNumber + 1 = (select max(RowNumber)
                            from (select t.reemployeeid,
                                         t.scantime,
                                         RANK() OVER(order by scantime) as RowNumber
                                    from tab t))

作者: programmerxiaocai   发布时间: 2011-12-01

引用 2 楼 money729 的回复:
提供你一个方法供参考
select * from (select * from ss ORDER BY CANTIME desc )
where rownum<=2
minus
select * from (select * from ss ORDER BY CANTIME desc )
where rownum<=1

作者: rfb0204421   发布时间: 2011-12-01

测试数据
SQL code

CREATE TABLE T26
(
    ReemployeeID VARCHAR2(20),
    ScanTime    DATE
);

INSERT INTO T26 VALUES('100000000088', to_date('2011-12-01 10:27:14', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO T26 VALUES('100000000088', to_date('2011-12-01 10:32:00', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO T26 VALUES('100000000088', to_date('2011-12-01 10:35:54', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO T26 VALUES('100000000104', to_date('2011-12-01 10:32:06', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO T26 VALUES('100000000088', to_date('2011-12-01 10:55:44', 'YYYY-MM-DD HH24:MI:SS'));
INSERT INTO T26 VALUES('100000000106', to_date('2011-12-01 10:56:33', 'YYYY-MM-DD HH24:MI:SS'));



结果

作者: LuiseRADL   发布时间: 2011-12-01