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

Home -> Community -> Usenet -> c.d.o.server -> Re: Subquery to return top row

Re: Subquery to return top row

From: Dmitry Pugachev <dev_at_ellink.ru>
Date: 2000/06/27
Message-ID: <8j9iv6$p26$1@news.sovam.com>#1/1

Hi,

see posted comments

> I need to have a subquery return a particular row; I can create a normal
> query to get several rows and I can get the row I need to come to the
> top with ORDER BY. However, when I try to embed this query as a
> subquery, I get a "missing right parenthesis" error on the ORDER
> keyword.
>
> First, is it possible to use ORDER BY in a subquery?

No, it's not!

>
> Second, I need to limit the results of the subquery to just the first
> row -- it is possible to somehow just select the top row? In other
> words, the row with rownum = 1?

You surely can limit the results just as you wrote but IMO that is not the case.

> Here is the skeleton SQL that I currently have. What I need is the
> goremal_email_address that floats to the top in the subquery. The ?????
> marks are things that I do not know how to fill in. And I have no idea
> how to select just the first row that is returned. I have seen other
> examples that indicate this can be done; I just cannot figure out the
> syntax to accomplish it.
>
> Help! Thank you!
>
> SELECT
> SUBSTR(spriden_first_name,1,12) "FIRST"
> ,SUBSTR(spriden_last_name,1,12) "LAST"
> ,goremal_email_address "E-MAIL" <-- need one of these
> FROM
> spriden,goremal
> WHERE
> goremal_pidm = spriden_pidm
> AND ????? =

                replace ??? with goremal_activity_date

> (
> SELECT ?????

                replace ??? with MAX(goremal_activity_date)

> FROM goremal
> WHERE goremal_pidm = spriden_pidm
> AND goremal_emal_code = 'EML'
> AND goremal_status_ind = 'A'
> AND goremal_preferred_ind = 'Y'

exclude ORDER BY!

> ORDER BY goremal_preferred_ind DESC,

This is of course common method and some additional conditions possible in your situation.
For example you can have more then one row with the same <goremal_activity_date> for one <goremal_pidm>. In this case you should place additional filter in query or use distinct to eliminate this.

>
>
> Sent via Deja.com http://www.deja.com/
> Before you buy.

hth,
Dmitry Pugachev, Oracle DBA Received on Tue Jun 27 2000 - 00:00:00 CDT

Original text of this message

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