Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdRUp-00011i-RM for pgsql-sql@arkaria.postgresql.org; Mon, 04 Nov 2013 21:15:40 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VdRUp-0005ZQ-7F for pgsql-sql@arkaria.postgresql.org; Mon, 04 Nov 2013 21:15:39 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdRUo-0005ZK-Jv for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 21:15:38 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VdRUl-0006el-LE for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 21:15:38 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1VdRUk-0004lY-PE for pgsql-sql@postgresql.org; Mon, 04 Nov 2013 13:15:34 -0800 Date: Mon, 4 Nov 2013 13:15:34 -0800 (PST) From: David Johnston To: pgsql-sql@postgresql.org Message-ID: <1383599734773-5776920.post@n5.nabble.com> In-Reply-To: <1383599125836-5776914.post@n5.nabble.com> References: <1383596556949-5776903.post@n5.nabble.com> <1383597496358-5776905.post@n5.nabble.com> <1383599125836-5776914.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 > 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; > -- CREATE TYPE http://www.postgresql.org/docs/9.3/interactive/sql-createtype.html Since you have a known set of columns you define a custom type that matches the structure and then: SELECT fk_item_header_id, json_agg( ( message_id, dt_create, ... )::custom_type ) FROM ...; Not tested as I do not have a JSON capable release available at the moment. But since the type information includes the column names the conversion to JSON should keep those names intact. David J. -- View this message in context: http://postgresql.1045698.n5.nabble.com/json-and-aggregate-tp5776903p5776920.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