Re: [SQL] Problem with aggregate functions and GROUP BY

2001-08-08 Thread Josh Berkus
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

Re: [SQL] Problem with aggregate functions and GROUP BY

2001-08-08 Thread Tom Lane
"Alex Page" <[EMAIL PROTECTED]> writes: > I'm trying to write a query that returns the rows containing the > most recent values for each foreign key. The best way to do this (on every dimension except portability :-() is SELECT DISTINCT ON. See the weather-report example in the SELECT reference