Re: Why can't I select un-grouped columns when grouping by a (non-primary) unique key?

From: Daniel Lenski <dlenski(at)gmail(dot)com>
To: pgsql-general <pgsql-general(at)postgresql(dot)org>
Subject: Re: Why can't I select un-grouped columns when grouping by a (non-primary) unique key?
Date: 2014-09-24 18:17:20
Message-ID: CAOw_LSGWP1kSs6BhUS0f2ngb=Cckm3MwN6Q9Wm77Jadpz10S7A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Sep 24, 2014 at 10:37 AM, Alberto Cabello Sánchez
<alberto(at)unex(dot)es> wrote:
> At first sight, primary key means no grouping at all, as there are no
> duplicated A.primary_key values:
>
> SELECT A.document
> FROM A
> GROUP BY A.primary_key
>
> is the same as
>
> SELECT A.document
> FROM A
>

Yes, my example is oversimplified, because GROUP BY primary_key has no
semantic effect on a single-table query, although it still illustrates
the different behavior for PRIMARY KEY vs. UNIQUE NOT NULL, which
should be logically equivalent.

The examples that Geoff Montee gave are better because (GROUP BY
primary_key) does change the meaning of the query when there's a join
to another table.

Dan

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2014-09-24 18:19:10 Re: Why can't I select un-grouped columns when grouping by a (non-primary) unique key?
Previous Message Daniel Lenski 2014-09-24 18:06:21 Re: Why can't I select un-grouped columns when grouping by a (non-primary) unique key?