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