home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / database / oracle / 2735 < prev    next >
Encoding:
Text File  |  1993-01-08  |  2.1 KB  |  57 lines

  1. Newsgroups: comp.databases.oracle
  2. Path: sparky!uunet!stanford.edu!kronos.arc.nasa.gov!maas-neotek.arc.nasa.gov!blah
  3. From: blah@maas-neotek.arc.nasa.gov (MJW/TWF)
  4. Subject: Re: 40 values in desc order
  5. Message-ID: <1993Jan8.212942.16642@kronos.arc.nasa.gov>
  6. Sender: usenet@kronos.arc.nasa.gov (Will Edgington, wedgingt@ptolemy.arc.nasa.gov)
  7. Nntp-Posting-Host: maas-neotek.arc.nasa.gov
  8. Organization: NASA Ames Research Center
  9. References: <6622.2b485a61@hayes.com> <1993Jan8.200238.534@osnbe.Olivetti.ch>
  10. Date: Fri, 8 Jan 1993 21:29:42 GMT
  11. Lines: 44
  12.  
  13. In article <1993Jan8.200238.534@osnbe.Olivetti.ch> rheiger@renext.eiger.olivetti.ch writes:
  14. >In article <6622.2b485a61@hayes.com> fgreene@hayes.com writes:
  15. >> In article <Bzo2t0.67@lut.fi>, hietanen@lut.fi (Pentti Hietanen) writes:
  16. >> > 
  17. >> > What kind of sql sentence should we use to get 40 values
  18. >> > from database in descending order?
  19. >> > 
  20. >> > Pentti Hietanen, student of Lappeenranta University of Technology, Finland.
  21. >> > internet: hietanen%lut.fi
  22. >> 
  23. >> 
  24. >> Probably the easiest way is to use the ROWNUM function.  For example,
  25. >> 
  26. >>     SELECT field1, field2, fieldn
  27. >>     FROM   the_table
  28. >>     WHERE  ROWNUM < 40;
  29. >> 
  30. >This will only limit the output to the first 40 rows. It will not order them in  
  31. >a predictible manner. If you want to order the rows descending you should  
  32. >probably use the "ORDER BY attr1, attr2... DESCENDING" clause. However this  
  33. >will not restrict the select to 40 rows. To do both you could
  34. >
  35. >select <attribute1>,....
  36. >from tablename
  37. >where key_attr_list in (
  38. >select key_attr_list
  39. >from tablename
  40. >order by order_attr_list descending)
  41. >where rownum < 40;
  42. >
  43. >This is probably rather SLOW. I am sure some SQL cracks will have a better  
  44. >solution.
  45.  
  46. Unfortunately, it won't even work, as you have an ORDER BY in a
  47. subquery, which is a nono.
  48.  
  49. There is no way to limit the number of rows selected, and use an ORDER
  50. BY, in the same SQL statement.  This is because the ROWNUM value is
  51. assigned before the rows are ordered by the ORDER BY clause.  
  52.  
  53. Note that, using a view, what you want to accomplish is not difficult
  54. at all.
  55.  
  56. Mark
  57.