Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
Home -> Community -> Usenet -> c.d.o.misc -> Re: [student] BQFH
Oliver,
MAX and SUM functions don't nest very well. If you want to return the film_name and subtot for the film with the MAX(subtot), one solution, although relatively ugly, would be:
SELECT F.FILM_NAME,
SUM(S2.CONC_SEATS_SOLD) + SUM(S2.ORD_SEATS_SOLD) GRAND_SUB FROM (SELECT MAX(SUBTOT) new_max FROM ( SELECT SUM(S.CONC_SEATS_SOLD) + SUM(S.ORD_SEATS_SOLD) SUBTOT FROM SCREENING S GROUP BY S.FILM_NO)) max_film, film f, screening s2
+ SUM(S2.ORD_SEATS_SOLD)
...this query will also pull out multiple records if more than one film's SUBTOT is maximum. You could also join the table to itself and get the top-n rows where n = 1, but that would be pretty slow on big tables.
Jay!!!
Oliver White wrote:
> I have been given much grief by this query, obviously the syntax is
> completely wrong, but I have tried putting the MAX function in all
> sorts of places. In this case I get a "SQL command not properly ended"
> error, but in other configurations the concentric grouping functions
> are not allowed. How would I find the maximum of the subtotals, what
> would be the syntax?
>
> SELECT F.FILM_NAME, SUM(S.CONC_SEATS_SOLD) + SUM(S.ORD_SEATS_SOLD)
> SUBTOT
> FROM FILM F, SCREENING S
> WHERE S.FILM_NO = F.FILM_NO
> ORDER BY S.FILM_NO
> HAVING SUBTOT = MAX(SUBTOT);
> --
> On the smeeter.
> MHM 22x9
> --
Received on Mon Oct 26 1998 - 12:02:29 CST