group by error message?

Started by Louis-David Mitterrandover 17 years ago3 messagesgeneral
Jump to latest
#1Louis-David Mitterrand
vindex+lists-pgsql-general@apartia.org

Hi,

Running this query:

critik=# select distinct pt.type_fr,sum(e2.id_event) from person_type pt natural join person_to_event join event e using (id_event) LEFT JOIN event e2 ON e.id_event = e2.id_event AND e2.id_event=219 join event_type et ON e.id_event_type = et.id_event_type where et.type_fr='th��tre' GROUP BY pt.type_fr IS NULL;

gives this error:

ERROR: column "pt.type_fr" must appear in the GROUP BY clause or be used in an aggregate function

But pt.type_fr is _already_ in the group by.

When adding e2.id_event however ("GROUP BY pt.type_fr,e2.id_event") it works.

Did I miss something there?

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Louis-David Mitterrand (#1)
Re: group by error message?

Louis-David Mitterrand <vindex+lists-pgsql-general@apartia.org> writes:

critik=# select distinct pt.type_fr,sum(e2.id_event) from person_type pt natural join person_to_event join event e using (id_event) LEFT JOIN event e2 ON e.id_event = e2.id_event AND e2.id_event=219 join event_type et ON e.id_event_type = et.id_event_type where et.type_fr='th��tre' GROUP BY pt.type_fr IS NULL;

gives this error:

ERROR: column "pt.type_fr" must appear in the GROUP BY clause or be used in an aggregate function

But pt.type_fr is _already_ in the group by.

No, "pt.type_fr IS NULL" is in the group by. That doesn't ensure that
each group has a unique value of pt.type_fr itself.

regards, tom lane

#3Louis-David Mitterrand
vindex+lists-pgsql-general@apartia.org
In reply to: Tom Lane (#2)
Re: group by error message?

On Thu, Sep 25, 2008 at 11:01:08AM -0400, Tom Lane wrote:

Louis-David Mitterrand <vindex+lists-pgsql-general@apartia.org> writes:

critik=# select distinct pt.type_fr,sum(e2.id_event) from person_type pt natural join person_to_event join event e using (id_event) LEFT JOIN event e2 ON e.id_event = e2.id_event AND e2.id_event=219 join event_type et ON e.id_event_type = et.id_event_type where et.type_fr='th��tre' GROUP BY pt.type_fr IS NULL;

gives this error:

ERROR: column "pt.type_fr" must appear in the GROUP BY clause or be used in an aggregate function

But pt.type_fr is _already_ in the group by.

No, "pt.type_fr IS NULL" is in the group by. That doesn't ensure that
each group has a unique value of pt.type_fr itself.

Oops, once more I jumped the gun a bit too fast. Thanks for catching it
Tom!

Best regards,