agora inbox for pgsql-sql@postgresql.org
help / color / mirror / Atom feedFrom: Diway <diway@diway.net>
To: pgsql-sql@postgresql.org
Subject: Re: json and aggregate
Date: Mon, 4 Nov 2013 13:05:25 -0800 (PST)
Message-ID: <1383599125836-5776914.post@n5.nabble.com> (raw)
In-Reply-To: <1383597496358-5776905.post@n5.nabble.com>
References: <1383596556949-5776903.post@n5.nabble.com>
<1383597496358-5776905.post@n5.nabble.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-sql>
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
view thread (4+ messages) latest in thread
Message-ID: <1383599125836-5776914.post@n5.nabble.com>
Permalink: ../1383599125836-5776914.post@n5.nabble.com/
Also on: postgresql.org/message-id/1383599125836-5776914.post@n5.nabble.com
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: diway@diway.net
Subject: Re: json and aggregate
In-Reply-To: <1383599125836-5776914.post@n5.nabble.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox