home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / database / oracle / 2741 < prev    next >
Encoding:
Internet Message Format  |  1993-01-09  |  2.5 KB

  1. Path: sparky!uunet!paladin.american.edu!howland.reston.ans.net!spool.mu.edu!agate!biosci!oracle!unrepliable!bounce
  2. From: mfriedma@uucp (Michael Friedman)
  3. Newsgroups: comp.databases.oracle
  4. Subject: Re: 40 values in desc order
  5. Message-ID: <1993Jan9.171338.27327@oracle.us.oracle.com>
  6. Date: 9 Jan 93 17:13:38 GMT
  7. References: <6622.2b485a61@hayes.com> <1993Jan8.200238.534@osnbe.Olivetti.ch>
  8. Sender: usenet@oracle.us.oracle.com (Oracle News Poster)
  9. Organization: Oracle Corporation
  10. Lines: 57
  11. Nntp-Posting-Host: appseq
  12. X-Disclaimer: This message was written by an unauthenticated user
  13.               at Oracle Corporation.  The opinions expressed are those
  14.               of the user and not necessarily those of Oracle.
  15.  
  16. In article <1993Jan8.200238.534@osnbe.Olivetti.ch> rheiger@renext.eiger.olivetti.ch writes:
  17. >In article <6622.2b485a61@hayes.com> fgreene@hayes.com writes:
  18. >> In article <Bzo2t0.67@lut.fi>, hietanen@lut.fi (Pentti Hietanen) writes:
  19.  
  20. >> > What kind of sql sentence should we use to get 40 values
  21. >> > from database in descending order?
  22.  
  23. >> > Pentti Hietanen, student of Lappeenranta University of Technology, Finland.
  24.  
  25. >> Probably the easiest way is to use the ROWNUM function.  For example,
  26.  
  27. >>     SELECT field1, field2, fieldn
  28. >>     FROM   the_table
  29. >>     WHERE  ROWNUM < 40;
  30.  
  31. >This will only limit the output to the first 40 rows. It will not order them in  
  32. >a predictible manner. If you want to order the rows descending you should  
  33. >probably use the "ORDER BY attr1, attr2... DESCENDING" clause. However this  
  34. >will not restrict the select to 40 rows. To do both you could
  35.  
  36. >select <attribute1>,....
  37. >from tablename
  38. >where key_attr_list in (
  39. >select key_attr_list
  40. >from tablename
  41. >order by order_attr_list descending)
  42. >where rownum < 40;
  43.  
  44. Sorry, but this will not work either.
  45.  
  46. To start with, you can't have an order by in a subquery.
  47.  
  48. Even if you could, since you are looking at the rownum in the outer
  49. query this would be no different than
  50.  
  51. select <attribute1>,...
  52. from tablename
  53. where rownum < 40
  54.  
  55. Finally, there is no order by in the outer query.
  56.  
  57. If the questioner just wants any 40 rows in descending order then
  58.  
  59. select <attribute1>,...
  60. from tablename
  61. where rownum <= 40
  62. order by key desc
  63.  
  64. will work.  If the questioner wants the top forty then...
  65.  
  66. select <attribute1>,...
  67. from tablename t1
  68. where 40 <= (select count(*) from tablename t2 where t2.key >= t1.key)
  69. order by key desc
  70. -- 
  71. -------------------------------------------------------------------------------
  72. I am not an official Oracle spokesman.  I speak for myself and no one else.
  73.