Re: BUG #8830: Query with a subquery failes to execute if this subquery does not contain references to own table

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: valgog(at)gmail(dot)com
Cc: pgsql-bugs(at)postgresql(dot)org
Subject: Re: BUG #8830: Query with a subquery failes to execute if this subquery does not contain references to own table
Date: 2014-01-14 15:13:16
Message-ID: 16075.1389712396@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

valgog(at)gmail(dot)com writes:
> select 'key.' || md5( s.i::text ) as key,
> 'value.' || ( select string_agg( md5( (s.i)::text), '' )
> from generate_series(1, 20) as g(j)
> ) as value
> from generate_series(1, 100) as s(i);

> fails with:

> ERROR: column "s.i" must appear in the GROUP BY clause or be used in an
> aggregate function
> LINE 2: select 'key.' || md5( s.i::text ) as key,

That's per SQL standard, AFAICS. The string_agg() call is an aggregate of
the outer query, *not* the sub-select, because the lowest-level variable
in the aggregate's argument is of the outer query. See SQL:2011
6.9 <set function specification> syntax rule 6:

6) The aggregation query of a <set function specification> SFS is determined as follows. Case:
a) If SFS has no aggregated column reference, then the aggregation query of SFS is the innermost <query specification> that contains SFS.
b) Otherwise, the innermost qualifying query of the aggregated column references of SFS is the aggregation query of SFS.

So the outer query has an aggregate, and that means that any variables
that aren't within the aggregate call have to be grouped columns.

regards, tom lane

In response to

Browse pgsql-bugs by date

  From Date Subject
Next Message M.Mamin 2014-01-14 16:51:28 BUG #8833: nested dynamic cursors may lead to type mismatch compared to the initial plan
Previous Message Ufuk Kayserilioglu 2014-01-14 13:59:29 Re: BUG #8821: pg_trgm segfault with Turkish locale database