|
|
|
|
|
|
|
Re: can i find nth row for particular table [message #230062 is a reply to message #229860] |
Tue, 10 April 2007 15:41   |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
if you wanted to get the fifth row after sorting by emp_number (for example), you could do something like
select *
from emp e,
(select x.emp_number,rownum rnum
from
(select emp_number from emp
order by emp_number) x) y
where y.emp_number = e.emp_number
and y.rnum = 5;
|
|
|
|
Re: can i find nth row for particular table [message #230065 is a reply to message #229860] |
Tue, 10 April 2007 15:53   |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
inner select sorts by order. The next select generates the rownum, which is generated before the order. and the outer select returns finds the fifth employee number and select the entire row from the emp table that matches the first emp number.
|
|
|
|
|
|
|
|
|
Re: can i find nth row for particular table [message #230171 is a reply to message #230161] |
Wed, 11 April 2007 02:24  |
 |
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
Michel Cadot wrote on Wed, 11 April 2007 09:03 | Don't follow you
| I must wake up. I probably mistyped. Here's how I can reproduce the error I got. Notice the row_id that has been changed into rowid.
In the mean time I got me a nice cup of coffee so I'll probably wake up in a couple of minutes.
select *
from emp e,
(select x.empno,x.rowid,rownum rnum
from
(select empno,rowid row_id from emp
order by empno) x) y
where y.rowid = e.rowid
and y.rnum = 5;
Silly me. Anyway, if I look at the plans I'd probably opt for another method, but then again: without the details it's hard to make a stand.
SELECT *
FROM emp e
, (SELECT x.empno
, x.row_id
, ROWNUM rnum
FROM (SELECT empno
, ROWID row_id
FROM emp
ORDER BY empno) x) y
WHERE y.row_id = e.ROWID AND y.rnum = 5
/
Plan
SELECT STATEMENT CHOOSE
Cost: 5 Bytes: 1,106 Cardinality: 14
6 HASH JOIN Cost: 5 Bytes: 1,106 Cardinality: 14
1 TABLE ACCESS FULL SCOTT.EMP Cost: 2 Bytes: 644 Cardinality: 14
5 VIEW SCOTT. Cost: 2 Bytes: 462 Cardinality: 14
4 COUNT
3 VIEW SCOTT. Cost: 2 Bytes: 280 Cardinality: 14
2 INDEX FULL SCAN UNIQUE SCOTT.EMP_PRIMARY_KEY Cost: 1 Bytes: 140 Cardinality: 14
SELECT *
FROM (SELECT empno
, ename
, job
, mgr
, hiredate
, sal
, comm
, deptno
, RANK () OVER (ORDER BY empno) rnum
FROM emp) x
WHERE x.rnum = 5;
Plan
SELECT STATEMENT CHOOSE
Cost: 2 Bytes: 1,54 Cardinality: 14
4 VIEW SCOTT. Cost: 2 Bytes: 1,54 Cardinality: 14
3 WINDOW NOSORT Cost: 2 Bytes: 546 Cardinality: 14
2 TABLE ACCESS BY INDEX ROWID SCOTT.EMP Cost: 2 Bytes: 546 Cardinality: 14
1 INDEX FULL SCAN UNIQUE SCOTT.EMP_PRIMARY_KEY Cost: 1 Cardinality: 14
MHE
[Updated on: Wed, 11 April 2007 02:35] Report message to a moderator
|
|
|