From: | "Josh Berkus" <josh(at)agliodbs(dot)com> |
---|---|
To: | "Alex Page" <alex(dot)page(at)solid-state-logic(dot)com> |
Cc: | pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Problem with aggregate functions and GROUP BY |
Date: | 2001-08-08 20:04:39 |
Message-ID: | web-99001@davinci.ethosmedia.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
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(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco
From | Date | Subject | |
---|---|---|---|
Next Message | Justin Clift | 2001-08-09 01:51:43 | Re: Re: Mirroring the database? |
Previous Message | Stephan Szabo | 2001-08-08 18:14:08 | Re: REFERENCES constraint |