🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.interbase.sql
Article #11359

Re: Selecting row in which a column value is maximum

#11359
From: Arno Brinkman
Date: Sun, 24 Nov 2002 13:19
30 lines
1960 bytes
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)

I'm afraid that the above query will be slow, so it's faster to use an SP.

Regards,
Arno
Technical Details
Message-ID: <3de0d0f0$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <3de051cf@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 05:33:36 PST
From: "Arno Brinkman" <bnewsgroups @ abvisie.nl>
Newsgroups: borland.public.interbase.sql
References: <3de051cf@newsgroups.borland.com>
Subject: Re: Selecting row in which a column value is maximum
Date: Sun, 24 Nov 2002 14:19:58 +0100
Lines: 33
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.50.4522.1200
X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4522.1200
NNTP-Posting-Host: 213.116.160.131
Message-ID: <3de0d0f0$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1038143728 213.116.160.131 (24 Nov 2002 05:15:28 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-03!sn-xit-06!sn-xit-04!supernews.com!news.maxwell.syr.edu!nntp-relay.ihug.net!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail