home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / database / oracle / 1442 < prev    next >
Encoding:
Internet Message Format  |  1992-08-31  |  1.3 KB

  1. Path: sparky!uunet!decwrl!bu.edu!dartvax!kip-sn-49.dartmouth.edu!user
  2. From: carl.pedersen@dartmouth.edu (L. Carl Pedersen)
  3. Newsgroups: comp.databases.oracle
  4. Subject: Re: SQL*plus -- limiting maximum number of returned rows?
  5. Message-ID: <carl.pedersen-310892182013@kip-sn-49.dartmouth.edu>
  6. Date: 31 Aug 92 22:26:50 GMT
  7. References: <BtuK40.8By@ef2007.efhd.ford.com>
  8. Sender: news@dartvax.dartmouth.edu (The News Manager)
  9. Followup-To: comp.databases.oracle
  10. Organization: Dartmouth College
  11. Lines: 25
  12.  
  13. In article <BtuK40.8By@ef2007.efhd.ford.com>, wwm@ef2007.efhd.ford.com
  14. (William Meahan) wrote:
  15. > In SQL*Plus (not embedded SQL!) is there any way of limiting the number
  16. > of rows returned by a query?
  17. > I am working on a report that is supposed to find the "top 5" items in 
  18. > a catagory.  The query returns 0 or more rows based on the selection
  19. > criteria, sorted in the appropriate order.  Some queries return a dozen
  20. > or more rows, but I'm only interested in the first 5.
  21.  
  22. In some cases, checking for ROWNUM <= 5 may work, but in general you can do
  23. something like:
  24.  
  25.     select *
  26.       from t a
  27.      where 5 >=
  28.            (select count(*) from t b
  29.              where b.value >= a.value );
  30.  
  31. This works if value is unique.  If it isn't, you can concatenate ROWID to
  32. it.
  33.  
  34. I make no guarantee that this will perform well, but the basic technique
  35. works.
  36.