Re: field must appear in the GROUP BY clause or be used - Mailing list pgsql-general

From Richard Huxton
Subject Re: field must appear in the GROUP BY clause or be used
Date
Msg-id 200402271700.28133.dev@archonet.com
Whole thread Raw
In response to Re: field must appear in the GROUP BY clause or be used  (Bill Moran <wmoran@potentialtech.com>)
List pgsql-general
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

pgsql-general by date:

Previous
From: Michael Chaney
Date:
Subject: Re: correlated delete with 'in' and 'left outer join'
Next
From: Mike Mascari
Date:
Subject: Re: correlated delete with 'in' and 'left outer join'