Article View: borland.public.interbase.sql
Article #11361Re: Selecting row in which a column value is maximum
From: Edward Diener
Date: Sun, 24 Nov 2002 15:27
Date: Sun, 24 Nov 2002 15:27
36 lines
2348 bytes
2348 bytes
"Arno Brinkman" <bnewsgroups @ abvisie.nl> wrote in message news:3de0d0f0$1@newsgroups.borland.com... > Hi, > > > How does one select a row in a table in which a column value is the > maximum > > value of all the rows ? The MAX(column_name) syntax does not appear that > it > > can be used in a WHERE clause but only in a SELECT clause to get the > maximum > > value itself. Instead I want all column values for a row where a > particular > > column has the maximum value. It seem as if the only way to do this is to > > first SELECT MAX(column_name) from ATABLE, and then use the value returned > > to to a SELECT * FROM ATABLE WHERE column_name = mymax_value . > > Indeed you can't use MAX,MIN,AVG etc. in the WHERE clause, but you can use > them in the HAVING clause. Using having in your case doesn't seem the > resolution. You can use a sub-query in the WHERE clause or write a stored > procedure with the solution you suggest. > > SELECT > * > FROM > ATable > WHERE > ATable.AField = (SELECT MAX(AField) FROM ATable) Thanks, I managed to realize that this use of sub-query was thew answer. > > I'm afraid that the above query will be slow, so it's faster to use an SP. OK, thanks for the hint to use a stored procedure instead.
Technical Details
Message-ID:
<3de0ef36$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!opentransit.net!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<3de051cf@newsgroups.borland.com> <3de0d0f0$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 114a0e,60a1aedc82f6444 X-Google-Attributes: gid114a0e,public X-Google-ArrivalTime: 2002-11-24 07:38:48 PST From: "Edward Diener" <eddielee@tropicsoft.com> Newsgroups: borland.public.interbase.sql References: <3de051cf@newsgroups.borland.com> <3de0d0f0$1@newsgroups.borland.com> Subject: Re: Selecting row in which a column value is maximum Date: Sun, 24 Nov 2002 10:27:23 -0500 Lines: 38 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 NNTP-Posting-Host: 216.175.65.175 Message-ID: <3de0ef36$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1038151478 216.175.65.175 (24 Nov 2002 07:24:38 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-05!sn-xit-01!sn-xit-04!supernews.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!opentransit.net!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail