On Friday 27 February 2004 16:39, Bill Moran wrote:
> John Sidney-Woollett wrote:
> > Bill Moran said:
> >>
> >>SELECT GCP.id,
> >> GCP.Name
> >> FROM Gov_Capital_Project GCP,
> >> WHERE TLM.TLI_ID = $2
> >> group by GCP.id
> >> ORDER BY gcp.name;
> >>ERROR: column "gcp.name" must appear in the GROUP BY clause or be used
> >> in an aggregate function
> Like I said, the most important part (to me) is to understand why
> Postgres refuses to run this. The fact that I don't know why points
> to an obvious lack of understanding on my account, and I'd like to
> remedy that :D
Like the error message says, if you're using GROUP BY everything in the SELECT
list must be an aggregate SUM(...) or used in the GROUP BY.
So, this is OK:
SELECT dept, week, SUM(amt_sold)
FROM weekly_sales
GROUP BY dept,week;
This isn't:
SELECT dept, week, SUM(amt_sold)
FROM weekly_sales
GROUP BY dept;
Ask yourself which "week" should be returned in the second case.
--
Richard Huxton
Archonet Ltd