Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdQuk-0006JF-Rv for pgsql-sql@arkaria.postgresql.org; Mon, 04 Nov 2013 20:38:23 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VdQuk-0000DQ-89 for pgsql-sql@arkaria.postgresql.org; Mon, 04 Nov 2013 20:38:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdQuj-0000DK-HY for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 20:38:21 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdQug-0005t5-0r for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 20:38:20 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1VdQue-0002TF-Bl for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 12:38:16 -0800 Date: Mon, 4 Nov 2013 12:38:16 -0800 (PST) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1383597496358-5776905.post@n5.nabble.com> In-Reply-To: <1383596556949-5776903.post@n5.nabble.com> References: <1383596556949-5776903.post@n5.nabble.com> Subject: Re: json and aggregate MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 1.8 (+) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org Diway wrote > Why is the following query not working ? > select sum((json_array_elements(data)->>'lines')::integer) as test from > test2 where id = 2; > ERROR: set-valued function called in context that cannot accept a set > > ('data' is obviously a json datatype) > > On the other side, this one is OK but I don't like it ;-) > select sum(value) from (select > (json_array_elements(data)->>'lines')::integer as value from test2 where > id = 2) x; How technical an answer do you want? Short answer is that GROUP BY/aggregates cannot process a set-returning-function (SRF) in the select-list. You have move the SRF into the associated FROM clause and let the individual rows feed from there into the GROUP BY/aggregates.