Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Mailing Lists -> Oracle-L -> Re: How to put a TOP 1 in a select

Re: How to put a TOP 1 in a select

From: Mark Richard <mrichard_at_transurban.com.au>
Date: Mon, 09 Jun 2003 20:44:49 -0700
Message-ID: <F001.005AE13E.20030609202438@fatcity.com>

Your english is fine, lets see if I understand the query...

Does it return the Item and Trans_Date of the record with the smallest quantity (ie: first row returned given that query is ordered by quantity)?

Oracle (at least version 8 - not sure about 9) doesn't really support this SQL but there are workarounds:

select * from (select item, trans_date
where company = 2000 and trans_date between (sysdate-14) and sysdate and doc_type = 'IS'
and reason_code = 'VTCL'
and substr(item, 2,2) = '57'
order by quantity)
where rownum <= 1;

This will probably return the correct result if I have understood the meaning of TOP 1 properly. Performance may or may not be an issue - although depending on how SQL Server implements this query you may not see much difference.

                                                                                       
                                                 
                      "Teresita Castro"                                                
                                                 
                      <[EMAIL PROTECTED]        To:       Multiple recipients of list 
ORACLE-L <[EMAIL PROTECTED]>                  
                      martmx.com>                cc:                                   
                                                 
                      Sent by:                   Subject:  How to put a TOP 1 in a 
select                                               
                      [EMAIL PROTECTED]                                                
                                               
                      om                                                               
                                                 
                                                                                       
                                                 
                                                                                       
                                                 
                      10/06/2003 11:19                                                 
                                                 
                      Please respond to                                                
                                                 
                      ORACLE-L                                                         
                                                 
                                                                                       
                                                 
                                                                                       
                                                 




Hi!!
I was working with SQL Server 2000, and now the company decided to change to Oracle. So I don't know much about Oracle, I am just reading a book and try to find information on the net.
I am trying to do the next query, but I guess Oracle did not have the function TOP. How can I do a TOP 1 in Oracle?

select TOP 1 ITEM, TRANS_DATE from ICTRANS where   (COMPANY = 2000)
 AND (TRANS_DATE BETWEEN (CURRENT_DATE-14) AND CURRENT_DATE)

 AND (DOC_TYPE = 'IS')
 AND (REASON_CODE = 'VTCL')
 AND (SUBSTR(ITEM, 2, 2) = '57')

order by QUANTITY

I hope you undestand my English.

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

   Privileged/Confidential information may be contained in this message.

          If you are not the addressee indicated in this message
       (or responsible for delivery of the message to such person),
            you may not copy or deliver this message to anyone.
In such case, you should destroy this message and kindly notify the sender
           by reply e-mail or by telephone on (61 3) 9612-6999.
   Please advise immediately if you or your employer does not consent to
                Internet e-mail for messages of this kind.
        Opinions, conclusions and other information in this message
              that do not relate to the official business of
                         Transurban City Link Ltd
         shall be understood as neither given nor endorsed by it.
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.net
-- 
Author: Mark Richard
  INET: [EMAIL PROTECTED]

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).
Received on Mon Jun 09 2003 - 22:44:49 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US