Re: [GENERAL] json aggregation question - Mailing list pgsql-general

From Paul Jungwirth
Subject Re: [GENERAL] json aggregation question
Date
Msg-id c9dbbd48-736c-1703-878d-5b1325fef3b8@illuminatedcomputing.com
Whole thread Raw
In response to [GENERAL] json aggregation question  (Chris Withers <chris@simplistix.co.uk>)
List 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



pgsql-general by date:

Previous
From: Sasa Vilic
Date:
Subject: Re: [GENERAL] Shared WAL archive between master and standby: WALs notalways identical
Next
From: Adrian Klaver
Date:
Subject: Re: [GENERAL] Shared WAL archive between master and standby: WALs notalways identical