Re: 7.3 "group by" issue

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: Dan Langille <dan(at)langille(dot)org>
Cc: Chad Thompson <chad(at)weblinkservices(dot)com>, <pgsql-sql(at)postgresql(dot)org>
Subject: Re: 7.3 "group by" issue
Date: 2003-02-22 02:09:49
Message-ID: Pine.LNX.4.44.0302220245460.2067-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Dan Langille writes:

> > SELECT element_id as wle_element_id, COUNT(watch_list_id)
> > FROM watch_list JOIN watch_list_element
> > ON watch_list.id = watch_list_element.watch_list_id
> > WHERE
> > watch_list.user_id = 1
> > GROUP BY wle_element_id

This works because the first select list item is mentioned in the GROUP BY
clause (using its output label, this is a PostgreSQL extension).

> Yes, that works. But so do these.
>
> SELECT watch_list_element.element_id as wle_element_id,
> COUNT(watch_list_id)
> FROM watch_list JOIN watch_list_element
> ON watch_list.id = watch_list_element.watch_list_id
> WHERE
> watch_list.user_id = 1
> GROUP BY watch_list_element.element_id

This works because the first select list item is mentioned in the GROUP BY
clause.

> SELECT element_id as wle_element_id, COUNT(watch_list_id)
> FROM watch_list JOIN watch_list_element
> ON watch_list.id = watch_list_element.watch_list_id
> WHERE
> watch_list.user_id = 1
> GROUP BY element_id

This works because the first select list item is mentioned in the GROUP BY
clause.

> The original situation which did not work is:
>
> SELECT watch_list_element.element_id as wle_element_id,
> COUNT(watch_list_id)
> FROM watch_list JOIN watch_list_element
> ON watch_list.id = watch_list_element.watch_list_id
> WHERE
> watch_list.user_id = 1
> GROUP BY element_id

This does not work because the first select list item references a column
inside a join, which is not (necessarily) mathematically identical to the
column that arrives outside of the join and is in the GROUP BY clause.
(Think of an outer join: the column outside the join might contain added
null values. Of course you are using an inner join, but the constructs
work the same either way.)

--
Peter Eisentraut peter_e(at)gmx(dot)net

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Peter Eisentraut 2003-02-22 02:10:10 Re: function defination help ..
Previous Message Dan Langille 2003-02-22 01:18:02 7.3 GROUP BY differs from 7.2