pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: valgog@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8830: Query with a subquery failes to execute if this subquery does not contain references to own table
Date: Tue, 14 Jan 2014 10:13:16 -0500
Message-ID: <16075.1389712396@sss.pgh.pa.us> (raw)
In-Reply-To: <20140114103219.30126.61565@wrigleys.postgresql.org>
References: <20140114103219.30126.61565@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

valgog@gmail.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


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (2+ messages)

Message-ID: <16075.1389712396@sss.pgh.pa.us>
Permalink:  ../16075.1389712396@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/16075.1389712396@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, valgog@gmail.com
  Subject: Re: BUG #8830: Query with a subquery failes to execute if this subquery does not contain references to own table
  In-Reply-To: <16075.1389712396@sss.pgh.pa.us>
  References: <20140114103219.30126.61565@wrigleys.postgresql.org> <16075.1389712396@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox