From: | Josh Berkus <josh(at)agliodbs(dot)com> |
---|---|
To: | vampyre5(at)yahoo(dot)com, pgsql-sql(at)postgresql(dot)org |
Subject: | Re: Help with query involving aggregation and joining. |
Date: | 2003-02-24 06:46:05 |
Message-ID: | 200302232246.05035.josh@agliodbs.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
Eddie,
> 1) Josh suggested the following query. (I have made
> minor changes by adding the table name to each field)
>
> SELECT history.id, history.courseId, course.name,
> MAX(history.submission) AS submission
> FROM history JOIN course ON history.courseId =
> course.Id
> GROUP BY history.id, history.courseId, course.name
> ORDER BY course.name;
>
> The results returned are:
> id | courseid | name | submission
> ----+----------+-----------+------------
> 2 | 102 | Chemistry | 2002-02-17
> 4 | 102 | Chemistry | 2002-02-22
> 3 | 104 | Maths | 2002-04-30
> 5 | 104 | Maths | 2002-03-15
> 6 | 104 | Maths | 2002-01-21
> 1 | 101 | Physics | 2002-01-20
Sorry, knew I was making it too simple. Try:
SELECT history.id, history.courseId, course.name, submission
FROM history JOIN course ON history.courseId =
course.Id
JOIN (select course_id, max(submission) as sub_max
FROM history GROUP BY course_id) hmax
ON (history.course_id = hmax.course_id AND history.submission = hmax.sub_max)
GROUP BY history.id, history.courseId, course.name
ORDER BY course.name;
--
Josh Berkus
Aglio Database Solutions
San Francisco
From | Date | Subject | |
---|---|---|---|
Next Message | Christoph Haller | 2003-02-24 09:30:16 | Re: Porting from db2 problem |
Previous Message | Eddie Cheung | 2003-02-24 05:18:36 | Re: Help with query involving aggregation and joining. |