ROWNUM problem...

ROWNUM problem...

Post by TurkBe » Fri, 01 May 1998 04:00:00



You'll probably get a more technical answer, but in essence 'rownum'
is not a value in the table and when you use it in a where statement (
other that a < , which sqlplus handles as a special case ) the query
fails.

Rownum is assigned as the record is returned and sqlplus is able to
read it and compare it to the < operator's value and stop when it has
been  reached - no other operator works with rownum ( you can't say
rownum > 10 for instance )


>Hello all,

>    When I type "select A from AAA" then 100 rows selected.
>    Now, I want to select the first 10 records. So I type "select A from
>AAA where rownum > 0 and rownum <= 10", then 10 rows selected.
>    Now, I want to select the NEXT 10 record by "select A from AAA where
>rownum > 10 and rownum <= 20", then no row selected.

>    Could anyone tell me why no row selected? And how to select the NEXT 10
>records by a SQL statement?

>Thanks,
>Jimmy

 
 
 

ROWNUM problem...

Post by Jimm » Sat, 02 May 1998 04:00:00


Hello all,

        When I type "select A from AAA" then 100 rows selected.
        Now, I want to select the first 10 records. So I type "select A from
AAA where rownum > 0 and rownum <= 10", then 10 rows selected.
        Now, I want to select the NEXT 10 record by "select A from AAA where
rownum > 10 and rownum <= 20", then no row selected.

        Could anyone tell me why no row selected? And how to select the NEXT 10
records by a SQL statement?

Thanks,
Jimmy

 
 
 

ROWNUM problem...

Post by ?yvind Hodn » Sat, 02 May 1998 04:00:00


The problem is that ROWNUM is assigned sequentially to each row that meets
the criteria. Oracle's SQL Language Reference Manual explains why no rows
are selected when you test for ROWNUM values greater than 1 (i.e. ROWNUM >
10):

 "The first row fetched is assigned a ROWNUM of 1 and makes the condition
false. The second row to be fetched is now the first row and is also
assigned a ROWNUM of 1 and also makes the condition false."

As to how you can retreive the next 10 rows, the easy way is to use PL/SQL,
but I suspect that's not what you want. I have never tried this in SQL, so I
have absolutely no idea whether it'll work, but you could try the following:

SELECT ROWNUM, MAX(a)
FROM aaa
GROUP BY ROWNUM
HAVING ROWNUM > 10

 
 
 

ROWNUM problem...

Post by Hans Duij » Tue, 05 May 1998 04:00:00


...you can use a view to do this, try the following:

create view vwtest as select rownum the_rownum,name from yourcolumn

...now you can make a 'subselection' with the query:

select * from vwtest where the_rownum between 10 and 20

However, now you probably will run into problems when you want to retrieve a
sorted selection.

Succes anyway

 
 
 

ROWNUM problem...

Post by TurkBe » Tue, 05 May 1998 04:00:00


A previous reply had a way to do it,
in case you missed it it involves using the 'minus' operator:

select * from table where rownum < 20....
minus
select * from table where <= 10

This will return rownums 11-20

Hope it helps,
John Greco
Oracle DBA

PS
The 'minus','intersect' and 'union' operators are underused and can be
very powerful for 'odd' queries that might otherwise require PL/SQL to
get the result you want.


>...you can use a view to do this, try the following:

>create view vwtest as select rownum the_rownum,name from yourcolumn

>...now you can make a 'subselection' with the query:

>select * from vwtest where the_rownum between 10 and 20

>However, now you probably will run into problems when you want to retrieve a
>sorted selection.

>Succes anyway

 
 
 

ROWNUM problem...

Post by Carlos Orozc » Wed, 08 Jul 1998 04:00:00


rownum is a pseudo column, the values are assigned after the data is
retrieved. You can use the having clause to get at this portion.

>Hello all,

> When I type "select A from AAA" then 100 rows selected.
> Now, I want to select the first 10 records. So I type "select A from
>AAA where rownum > 0 and rownum <= 10", then 10 rows selected.
> Now, I want to select the NEXT 10 record by "select A from AAA where
>rownum > 10 and rownum <= 20", then no row selected.

> Could anyone tell me why no row selected? And how to select the NEXT 10
>records by a SQL statement?

>Thanks,
>Jimmy

 
 
 

1. same sql, 160ms with rownum 13968, 25s with rownum 13969

hi,

without add a rownum in my sql query, i have in result '17731' and
execution time is 1m26s .
when i add a rownum (and rownum <= xx) to 13968 or inferior, it take 160
ms to execute. But when i put 13969 to rownum, it take 25s !!

here statistic for each query :

with 'and rownum <= 13968' :

           0  recursive calls
           4  db block gets
         135  consistent gets
           0  physical reads
           0  redo size
         379  bytes sent via SQL*Net to client
         424  bytes received via SQL*Net from client
           2  SQL*Net roundtrips to/from client
           6  sorts (memory)
           0  sorts (disk)
           1  rows processed

with 'and rownum <= 13969' :

           0  recursive calls
        3916  db block gets
       62341  consistent gets
           0  physical reads
           0  redo size
         379  bytes sent via SQL*Net to client
         424  bytes received via SQL*Net from client
           2  SQL*Net roundtrips to/from client
         984  sorts (memory)
           0  sorts (disk)
           1  rows processed

the executin plan is the same for both query.

With sql analyse, i have ask to generate a index script. after execute
the generated sql script for index, executions times with or without
rownum are very fast.

Now, i would like to know exactly why there is a big execution time
difference between these 2 sql query.  How determine the problem ?
analyse tools ? How know if a table or index need a rebuild ?

I use oracle 8.1.6, size of my base is only 80M.

sql:
select count(idapplicant) from applicants WHERE
  applicants.idapplicantstatus != '3' AND applicants.idapplicantstatus !='6'
AND applicants.idapplicantstatus != '12' AND Applicants.Typeofjobkey3 = '1'
  AND Applicants.Applicantentrydate >= '03-AOU-2001'
  AND  (
  Applicants.Solicitationtype ='0' OR
  Applicants.Idapplicant IN
   (SELECT Applications.Idapplicant FROM Applications WHERE
Applications.Applicationtreated ='2'
   MINUS
   SELECT Applications.Idapplicant FROM Applications,Applicants
   WHERE Applicants.Idapplicant=Applications.Idapplicant
   and Applicants.Solicitationtype ='1' and
Applications.Applicationtreated !='2')
  OR ( Applicants.Solicitationtype ='1' AND Applicants.Idapplicantstatus
='12' )
)

thx
seb

2. 4704-CO-COLORADO SPRGS-RDBMS-ORACLE-DBA Skills-Database Administrator

3. Problem with ROWNUM and REF CURSOR

4. SIMPLE(Probably) Paradox problem......SCREAM! Any help appreciated

5. big problem with ROWNUM!!! =(

6. Enterprise Manager x MSDE Again.....

7. Function like Oracle 'rownum'

8. Linux

9. ROWNUM as in Oracle ??

10. RowNum in SQL-S 6.5

11. Rownum

12. ROWNUM in SQLServer

13. (URGENT)How can I update a row according rownum