Re: Subquery uses ungrouped column

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Alex Ignatov <a(dot)ignatov(at)postgrespro(dot)ru>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: Subquery uses ungrouped column
Date: 2016-05-26 16:32:34
Message-ID: CAKFQuwZAZ4C8F1=2tsLJ4ut=t8KyFGFaV5yh5ZcQzr8xHjGZpA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Thu, May 26, 2016 at 12:02 PM, Alex Ignatov <a(dot)ignatov(at)postgrespro(dot)ru>
wrote:

> Hello!
>
> Why the following query:
>
> SELECT (select msc_id
> from collectors
> where id = substring(fileid from -1)
> ) msc_id
> from ip_data_records
> group by substring(fileid from -1)
>
> gives me:
>
> ERROR: subquery uses ungrouped column "ip_data_records.fileid" from outer
> query
> LINE 3: where id = substring(fileid from -1)
>
> but the following query:
>
> SELECT (select msc_id
> from collectors
> where id = fileid
> ) msc_id
> from ip_data_records
> group by fileid
> is working ok
>

From observation ​PostgreSQL​

​doesn't recognize the equivalency of the outer "group by substring(fileid
from -1)" and the subquery expression. What PostgreSQL does is push the
column ip_data_records.fieldid​ into the subquery where it just happens to
be used in the expression "substring(fileid from -1)". For all PostgreSQL
cares the subquery could have the expression "where id = lower(fileid)" and
the execution mechanics, and error, would be identical.

IOW, columns are the unit of interchange between a parent query and its
correlated subqueries.​

David J.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2016-05-26 16:44:51 Re: [GENERAL] Permission Denied Error on pg_xlog/RECOVERYXLOG file
Previous Message Alex Ignatov 2016-05-26 16:02:15 Subquery uses ungrouped column