Re: json and aggregate - Mailing list pgsql-sql

From David Johnston
Subject Re: json and aggregate
Date
Msg-id 1383599734773-5776920.post@n5.nabble.com
Whole thread Raw
In response to Re: json and aggregate  (Diway <diway@diway.net>)
List pgsql-sql
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.



pgsql-sql by date:

Previous
From: Diway
Date:
Subject: Re: json and aggregate
Next
From: George Woodring
Date:
Subject: Query specific table using relative position in search path