Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdRL1-0000Er-Cb for pgsql-sql@arkaria.postgresql.org; Mon, 04 Nov 2013 21:05:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VdRL0-0008Dn-O1 for pgsql-sql@arkaria.postgresql.org; Mon, 04 Nov 2013 21:05:30 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdRKz-0008Dh-U2 for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 21:05:30 +0000 Received: from sam.nabble.com ([216.139.236.26]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdRKw-0002xV-FY for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 21:05:29 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1VdRKv-0004Ax-RG for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 13:05:25 -0800 Date: Mon, 4 Nov 2013 13:05:25 -0800 (PST) From: Diway To: pgsql-sql@postgresql.org Message-ID: <1383599125836-5776914.post@n5.nabble.com> In-Reply-To: <1383597496358-5776905.post@n5.nabble.com> References: <1383596556949-5776903.post@n5.nabble.com> <1383597496358-5776905.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: -0.1 (/) 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 David Johnston wrote > 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. > > From the documentation: > > json_array_elements(json) [returns] SETOF json > > the "SETOF" is the indicator of a SRF. The reference to "set-valued > function" is another term for this concept. > > So, yes, the version you do not like as well is required. First you break > apart the json THEN you can aggregate. > > David J. Ok, thanks! I'm pretty new to postgres "advanced" features an didn't even know what a SRF was :) Another question if you don't mind, I've written the following function to convert many field to a json field and then group them based on another id. I don't really it because I have to query each line... --- CREATE OR REPLACE FUNCTION fn_tojson() RETURNS TABLE(id integer, data json) AS $$ DECLARE current_id integer; BEGIN FOR current_id IN( SELECT nah.id FROM item_header nah ) LOOP id := current_id; data := (SELECT json_agg(x) FROM (SELECT message_id, dt_created, lines, size, current_part, status, fk_item_cat_id FROM item na WHERE na.fk_item_header_id = $1) x); RETURN NEXT; END LOOP; END; $$ LANGUAGE 'plpgsql' VOLATILE; --- I could use something like this (not tested!) -- select fk_item_header_id as id, json_agg(row(message_id, dt_created, lines, size, current_part, status, fk_item_cat_id)) as data from item group by fk_item_header_id; -- but my json object keys would be translated to f1, f2, ... and I really need to keep the original ones. Thanks again! -- View this message in context: http://postgresql.1045698.n5.nabble.com/json-and-aggregate-tp5776903p5776914.html Sent from the PostgreSQL - sql mailing list archive at Nabble.com. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql