|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
Oracle question I have not been able to Google
I am making a web app where queries may return 1000+ records. I want to do something very common--divide the results into pages.
in other words I want LIMIT 100, 100 or something equivalent in oracle. I have seen this question asked in this forum and responsed have been to google for ROW_NUMBER(). however I have not found an example on Google that does exactly what i want. I have tried SELECT foo, ROW_NUMBER() OVER (ORDER BY bar) AS num FROM bar WHERE num > 99 AND num < 200 which of course hasn't worked. -------------------------------- ok I found this in another post: select table_name, n from ( select table_name, rownum n from ( select table_name from all_tables order by table_name) ) where n >= 2 and n <= 4 I am somewhat new to DB's and am confused about table_name and all_tables. lets say I have a table called 'foo', i want to select one column called 'bar', and I want rows >= 2 and <= 4. also, would the above query be slow? because it looks like you are doing 3 queries. Last edited by sad.machine : July 15th, 2004 at 12:14 AM. |
|
#2
|
||||
|
||||
|
Something like:
Code:
SELECT * FROM (SELECT ename, sal FROM scott.emp ORDER BY sal DESC) WHERE rownum >2 AND rownum < 5 should do the trick.
__________________
My blog about OpenSource Databases PDF tutorials about OSS databases, DBMonster ... Please contribute to Open Source Development, fill bug reports!!! Developer Shed eSupport Commented my.ini/my.cnf (PLEASE ADD YOUR OWN CONFIG TRICK) An introduction to database normalization Natural or Surrogate key Custom ordering for your results Correlated and uncorrelated subqueries Don't turn your outer joins into inner joins |
|
#3
|
|||
|
|||
|
Pablo- I see you and Scott/Tiger are best buddies.
![]() Personally, I never use those examples unless someobody is working in that context already. |
|
#4
|
||||
|
||||
|
Personally I always try to give examples, with data available to everyone, so -> scott/tiger (or EMPLOYEE.FDB in Firebird, or Northwind in MsSQL).
I try be more specific only if someone gives me a sql script to generate and populate structures and this happens once in a thousand posts. |
|
#5
|
|||
|
|||
|
thanks pabloj, but i have a question.
one of my concerns is performance. i have 1000+ records, and I want to page through them 100 at a time for performance reasons. would using your query help me in performance? because it seems like the subquery Code:
SELECT ename, sal FROM scott.emp ORDER BY sal DESC would select all 1000 records, then the outer query would filter through this. |
|
#6
|
|||
|
|||
|
Maybe its a stupid mistake on my part... or maybe i am overlooking something .... could someone tell me what am i doing wrong here ....
this query select count(*) from table_a where rownum = 1 ; returns me 1.. but when i give select count(*) form table_a where rownum = 2000; returns me 0 (i have 3600 rows in the table incase someone tought on those lines) neither does this work select count(*) from table_a where rownum > 1 and rownum < 200; Could someone tell me what i am doing wrong here? |
|
#7
|
|||
|
|||
|
rownum is an artificial column generated AFTER rows are selected.
You are effectively saying 'Take out the second marble from the box but not the first.' But you can't take out the second marble until you have taken out the first. You can try select * from (select x.*,rownum rn from x) where rn > 1000 |
![]() |
| Viewing: Dev Shed Forums > Databases > Oracle Development > Oracle question I have not been able to Google |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|