Re: Help with query involving aggregation and joining. - Mailing list pgsql-sql

From Josh Berkus
Subject Re: Help with query involving aggregation and joining.
Date
Msg-id 200302231224.47583.josh@agliodbs.com
Whole thread Raw
In response to Help with query involving aggregation and joining.  (Eddie Cheung <vampyre5@yahoo.com>)
List pgsql-sql
Eddie,

> My requirements involve several large tables. I have
> simplied scenario into the follow two fictional tables
> which describes the same requirements.

Keep in mind that a simplified example may not solve your real problem ...

> Basically I would like to display the latest
> submission for each course in a table as shown below,
> order by name of the courses.
>
> Query Results:
> ==============
>  id | courseId |  name    | submission
> ---------------------------------------
>  4  |  102     | Chemisty | 2002-02-22
>  3  |  104     | Maths    | 2002-04-30
>  1  |  101     | Physics  | 2002-01-20

Easy:

SELECT id, courseid, name, max(submission) as submission
FROM history JOIN courses ON history.courseid = course.id
GROUP BY id, courseid, name
ORDER BY name

And as such, I suspect that your real case is more complicated than the above
...

--
Josh Berkus
Aglio Database Solutions
San Francisco


pgsql-sql by date:

Previous
From: Josh Berkus
Date:
Subject: Re: [BUGS] 7.3 GROUP BY differs from 7.2
Next
From: Bruno Wolff III
Date:
Subject: Re: Help with query involving aggregation and joining.