Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W35gG-000464-8p for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Jan 2014 15:13:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W35gF-00051X-PY for pgsql-bugs@arkaria.postgresql.org; Tue, 14 Jan 2014 15:13:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W35gF-00051R-7X for pgsql-bugs@postgresql.org; Tue, 14 Jan 2014 15:13:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W35g9-0005ng-T3 for pgsql-bugs@postgresql.org; Tue, 14 Jan 2014 15:13:26 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id s0EFDGta016076; Tue, 14 Jan 2014 10:13:16 -0500 From: Tom Lane 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 In-reply-to: <20140114103219.30126.61565@wrigleys.postgresql.org> References: <20140114103219.30126.61565@wrigleys.postgresql.org> Comments: In-reply-to valgog@gmail.com message dated "Tue, 14 Jan 2014 10:32:19 +0000" Date: Tue, 14 Jan 2014 10:13:16 -0500 Message-ID: <16075.1389712396@sss.pgh.pa.us> X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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 syntax rule 6: 6) The aggregation query of a SFS is determined as follows. Case: a) If SFS has no aggregated column reference, then the aggregation query of SFS is the innermost 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