Re: Selecting a JSON object of arrays from a PostgreSQL table

2018-01-02 Thread Alexander Farber
Also got this nice suggestion at https://stackoverflow.com/q/48050127/165071 - SELECT COALESCE( json_object_agg( gid, array_to_json(y) ), '{}'::json) FROM( SELECT gid, array_agg( json_build_object( 'uid', uid, 'created',

Re: Selecting a JSON object of arrays from a PostgreSQL table

2018-01-01 Thread Alexander Farber
Hi Ivan, On Mon, Jan 1, 2018 at 3:34 PM, Ivan E. Panchenko < i.panche...@postgrespro.ru> wrote: > > select json_object_agg(gid, y) from > ( > select gid, jsonb_agg(row_to_json(chat)) y > from chat > group by gid > ) x; > > > 01.01.2018 16:41, Alexander Farber

Re: Selecting a JSON object of arrays from a PostgreSQL table

2018-01-01 Thread Ivan E. Panchenko
Hi Alex! Why not: select json_object_agg(gid, y) from     ( select gid, jsonb_agg(row_to_json(chat)) y from chat group by gid     ) x; Regards, Ivan 01.01.2018 16:41, Alexander Farber пишет: Hello and happy new year! I have prepared a simple SQL Fiddle

Selecting a JSON object of arrays from a PostgreSQL table

2018-01-01 Thread Alexander Farber
Hello and happy new year! I have prepared a simple SQL Fiddle demonstrating my problem: http://sqlfiddle.com/#!17/2c9c5/1 In a two-player game I store user chats in a table: CREATE TABLE chat( gid integer,/* game id */ uid integer,/* user id */ created