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

  1. Path: sparky!uunet!oracle!unrepliable!bounce
  2. Newsgroups: comp.databases.oracle
  3. From: mfriedma@uucp (Michael Friedman)
  4. Subject: Re: 40 values in desc order
  5. Message-ID: <1993Jan5.200712.12169@oracle.us.oracle.com>
  6. Sender: usenet@oracle.us.oracle.com (Oracle News Poster)
  7. Nntp-Posting-Host: appseq
  8. Organization: Oracle Corporation
  9. References: <Bzo2t0.67@lut.fi> <6622.2b485a61@hayes.com>
  10. Date: Tue, 5 Jan 1993 20:07:12 GMT
  11. X-Disclaimer: This message was written by an unauthenticated user
  12.               at Oracle Corporation.  The opinions expressed are those
  13.               of the user and not necessarily those of Oracle.
  14. Lines: 33
  15.  
  16. In article <6622.2b485a61@hayes.com> fgreene@hayes.com writes:
  17. >In article <Bzo2t0.67@lut.fi>, hietanen@lut.fi (Pentti Hietanen) writes:
  18.  
  19. >> What kind of sql sentence should we use to get 40 values
  20. >> from database in descending order?
  21.  
  22. >> Pentti Hietanen, student of Lappeenranta University of Technology, Finland.
  23. >> internet: hietanen%lut.fi
  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. First off, you need to do an order by.
  32.  
  33. Secondly, unless you are satisfied with any 40 this will not work
  34. because rownum is assigned before the order_by.
  35.  
  36. If you want the top 40 then try
  37.  
  38. Select fields
  39. From   the_table t1
  40. Where  40 >= (select count(*) from the_table t2 where 
  41.             t2.order_field > t1.order_field)
  42. Order by order_field DESC
  43.  
  44. Good Luck
  45.     
  46. -- 
  47. -------------------------------------------------------------------------------
  48. I am not an official Oracle spokesman.  I speak for myself and no one else.
  49.