Re: json aggregation question

From: Paul Jungwirth <pj(at)illuminatedcomputing(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: json aggregation question
Date: 2017-02-28 22:59:15
Message-ID: c9dbbd48-736c-1703-878d-5b1325fef3b8@illuminatedcomputing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 02/28/2017 08:21 AM, Chris Withers wrote:
> How can I aggregate the results of a query that equates to "show me the
> number of matching rows and the set of |tag1| value that have
> a |tag2| value of |t2val1|?
>
> ...but I really want:
>
> |count |tag1 -------+-------------------------2|["val1","val2","val3"](1row)|

Seems like this does the trick?:

SELECT COUNT(DISTINCT id),
json_agg(DISTINCT elem)
FROM (
SELECT id,
jsonb_array_elements_text(tags->'tag1') AS elem
FROM thing
WHERE tags->'tag2'?'t2val1'
) x;

You are looking to get always one result, right?

Yours,
Paul

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2017-03-01 00:15:49 Re: Shared WAL archive between master and standby: WALs not always identical
Previous Message Sasa Vilic 2017-02-28 22:55:39 Re: Shared WAL archive between master and standby: WALs not always identical