home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.databases.oracle
- Path: sparky!uunet!stanford.edu!kronos.arc.nasa.gov!maas-neotek.arc.nasa.gov!blah
- From: blah@maas-neotek.arc.nasa.gov (MJW/TWF)
- Subject: Re: 40 values in desc order
- Message-ID: <1993Jan8.212942.16642@kronos.arc.nasa.gov>
- Sender: usenet@kronos.arc.nasa.gov (Will Edgington, wedgingt@ptolemy.arc.nasa.gov)
- Nntp-Posting-Host: maas-neotek.arc.nasa.gov
- Organization: NASA Ames Research Center
- References: <6622.2b485a61@hayes.com> <1993Jan8.200238.534@osnbe.Olivetti.ch>
- Date: Fri, 8 Jan 1993 21:29:42 GMT
- Lines: 44
-
- In article <1993Jan8.200238.534@osnbe.Olivetti.ch> rheiger@renext.eiger.olivetti.ch writes:
- >In article <6622.2b485a61@hayes.com> fgreene@hayes.com writes:
- >> In article <Bzo2t0.67@lut.fi>, hietanen@lut.fi (Pentti Hietanen) writes:
- >> >
- >> > What kind of sql sentence should we use to get 40 values
- >> > from database in descending order?
- >> >
- >> > Pentti Hietanen, student of Lappeenranta University of Technology, Finland.
- >> > internet: hietanen%lut.fi
- >>
- >>
- >> Probably the easiest way is to use the ROWNUM function. For example,
- >>
- >> SELECT field1, field2, fieldn
- >> FROM the_table
- >> WHERE ROWNUM < 40;
- >>
- >This will only limit the output to the first 40 rows. It will not order them in
- >a predictible manner. If you want to order the rows descending you should
- >probably use the "ORDER BY attr1, attr2... DESCENDING" clause. However this
- >will not restrict the select to 40 rows. To do both you could
- >
- >select <attribute1>,....
- >from tablename
- >where key_attr_list in (
- >select key_attr_list
- >from tablename
- >order by order_attr_list descending)
- >where rownum < 40;
- >
- >This is probably rather SLOW. I am sure some SQL cracks will have a better
- >solution.
-
- Unfortunately, it won't even work, as you have an ORDER BY in a
- subquery, which is a nono.
-
- There is no way to limit the number of rows selected, and use an ORDER
- BY, in the same SQL statement. This is because the ROWNUM value is
- assigned before the rows are ordered by the ORDER BY clause.
-
- Note that, using a view, what you want to accomplish is not difficult
- at all.
-
- Mark
-