From: Josh Berkus <josh@agliodbs.com>
To: Alex Page <alex.page@solid-state-logic.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Problem with aggregate functions and GROUP BY
Date: Wed, 08 Aug 2001 13:04:39 -0700
Message-ID: <web-99001@davinci.ethosmedia.com> (raw)
In-Reply-To: <02ac01c12025$78644300$7fc472c1@solidstatelogic.com>
Alex,
> which is the highest value of 'sortby' for each value of 'fk'.
>
> I would think that the SQL to achieve this is:
>
> SELECT * FROM test GROUP BY fk HAVING sortby = MAX(sortby);
You need a subselect for this:
SELECT test.*
FROM test, (SELECT max(sortby) as maxsort, fk
FROM test
GROUP BY fk) maxtest
WHERE test.fk = maxtest.fk
AND test.sortby = maxtest.maxsort;
There's also a pgsql extension called SELECT DISTINCT ON, but it's not
SQL 92 standard so I don't reccomend it.
-Josh
______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh@agliodbs.com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: josh@agliodbs.com, alex.page@solid-state-logic.com
Subject: Re: Problem with aggregate functions and GROUP BY
In-Reply-To: <web-99001@davinci.ethosmedia.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox