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: SQL Query Help

Re: SQL Query Help

From: Mark Richard <mrichard_at_transurban.com.au>
Date: Wed, 28 May 2003 19:39:43 -0800
Message-ID: <F001.005A520D.20030528193943@fatcity.com>


This latest alternative will clearly only work when the latest id is known - I doubt this is reality.

There was a mention that the actual query is more complex - without seeing that complexity it's hard to offer further advice. RWB has provided the correct approach normally taken. If the date is indexed then Oracle can determine the max date very easily - the query looks worse than reality.

Another approach (which is normally worse though) is to order the query by date then wrap a "select * from (inner_query} where rownum = 1" around it. It's clunky though and probably isn't what you are after.

                                                                                                                                      
                      "Daniel W. Fink"                                                                                                
                      <optimaldba_at_yahoo        To:       Multiple recipients of list ORACLE-L <ORACLE-L_at_fatcity.com>                  
                      .com>                    cc:                                                                                    
                      Sent by:                 Subject:  Re: SQL Query Help                                                           
                      root_at_fatcity.com                                                                                                
                                                                                                                                      
                                                                                                                                      
                      29/05/2003 12:54                                                                                                
                      Please respond to                                                                                               
                      ORACLE-L                                                                                                        
                                                                                                                                      
                                                                                                                                      




SELECT ID,MAX(LastModDate)
FROM Tab
where id = 1
group by ID;

--
Daniel W. Fink
http://www.optimaldba.com


Basavaraja, Ravindra wrote:


>I had tried this as per Oracle Metalink Note.When I add the group by ID it
will display both the records like
>2 1/20/2003 2:56:18 AM
>1 4/23/2003 10:26:42 PM
>
>but I want to see the only the record with the latest time stamp like
>1 4/23/2003 10:26:42 PM
>
>-----Original Message-----
>Sent: Wednesday, May 28, 2003 6:25 PM
>To: Multiple recipients of list ORACLE-L
>
>
>Add "group by ID;" as in:
>
>
>SELECT ID,MAX(LastModDate) FROM Tab
>group by ID;
>
>
>
>>-----Original Message-----
>>From: Basavaraja, Ravindra [mailto:Ravindra.Basavaraja_at_T-Mobile.com]
>>Sent: Wednesday, May 28, 2003 8:15 PM
>>To: Multiple recipients of list ORACLE-L
>>Subject: SQL Query Help
>>
>>
>>i have a query that returns 2 rows with one column being id
>>and the other
>>being date-time stamp.
>>
>>i want to select the row with the latest timestamp among those two
>>records.they have difference id values
>>
>>SELECT ID,LastModDate
>>FROM Tab
>>
>>ID LastModDate
>>-- -----------
>>2 1/20/2003 2:56:18 AM
>>1 4/23/2003 10:26:42 PM
>>
>>I want to modify the above query to return the row with id=1
>>which has the
>>latest timestamp
>>
>>I tried this
>>
>>SELECT ID,MAX(LastModDate)
>>FROM Tab
>>
>>getting this error
>>OERR: ORA 937 not a single-group group function
>>
>>how do i get this work.
>>
>>Thanks
>>
>>
>>
-- Please see the official ORACLE-L FAQ: http://www.orafaq.net -- Author: Daniel W. Fink INET: optimaldba_at_yahoo.com 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: ListGuru_at_fatcity.com (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). <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<---->>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 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: mrichard_at_transurban.com.au 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: ListGuru_at_fatcity.com (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 Wed May 28 2003 - 22:39:43 CDT

Original text of this message

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