Oracle Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesOracle Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old July 15th, 2004, 12:06 AM
sad.machine sad.machine is offline
I hate nerds
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Posts: 533 sad.machine Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 20 h 48 m 41 sec
Reputation Power: 0
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.

Reply With Quote
  #2  
Old July 15th, 2004, 02:56 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,710 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 4 h 21 m 49 sec
Reputation Power: 259
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.

Reply With Quote
  #3  
Old July 15th, 2004, 09:31 AM
jim mcnamara jim mcnamara is offline
......@.........
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Posts: 1,307 jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level)jim mcnamara User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Week 3 Days 4 h 28 m 57 sec
Reputation Power: 48
Pablo- I see you and Scott/Tiger are best buddies.

Personally, I never use those examples unless someobody is working in that context already.

Reply With Quote
  #4  
Old July 15th, 2004, 09:38 AM
pabloj's Avatar
pabloj pabloj is offline
Modding: Oracle MsSQL Firebird
Dev Shed God 6th Plane (7500 - 7999 posts)
 
Join Date: Jun 2001
Location: Outside US
Posts: 7,710 pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level)pabloj User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 6 Days 4 h 21 m 49 sec
Reputation Power: 259
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.

Reply With Quote
  #5  
Old July 16th, 2004, 12:46 AM
sad.machine sad.machine is offline
I hate nerds
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jul 2003
Posts: 533 sad.machine Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 20 h 48 m 41 sec
Reputation Power: 0
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.

Reply With Quote
  #6  
Old July 20th, 2004, 10:57 PM
rshekdar rshekdar is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 2 rshekdar User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Unhappy Whats wrong here?

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?

Reply With Quote
  #7  
Old July 21st, 2004, 06:59 PM
gamyers gamyers is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 14 gamyers User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesOracle Development > Oracle question I have not been able to Google


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway