Search Postgresql Archives

Re: Returning values from an array of JSONB objects.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 4/13/16, Vitaly Burovoy <vitaly.burovoy@xxxxxxxxx> wrote:
> On 4/13/16, sighup <rts@xxxxxxxxx> wrote:
>> Hi, please excuse either my stupidity or naivety regarding this but I'm
>> a bit confused. Give the following basic table structure :
>>
>> TABLE Data (
>> 	ID INT NOT NULL,
>> 	Markers jsonb NOT NULL
>> );
>>
>> And the following data:
>>
>> INSERT INTO Data (ID, Markers) VALUES(1, '[ {"idle": true, "items": 8,
>> "done": 0}, {"idle": true, "items": 8, "done": 0}]') ;

Prerequisite:
CREATE TYPE myrowtype AS (idle bool, items int, done int);

Couple examples:

>> How can I extract the value of the 'items' key either as two rows

SELECT (jsonb_populate_recordset(NULL::myrowtype, Markers)).items
FROM Data WHERE (ID = X);

>> and or a sum of both.

SELECT sum(items) FROM(
    SELECT (jsonb_populate_recordset(NULL::myrowtype, Markers)).items
    FROM Data WHERE (ID = X)
)AS t;

> You should use a proper function "jsonb_populate_recordset" [1].
>
> [1] http://www.postgresql.org/docs/9.5/static/functions-json.html

-- 
Best regards,
Vitaly Burovoy


-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux