Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eW1Ae-0004vc-L2 for pgsql-general@arkaria.postgresql.org; Mon, 01 Jan 2018 14:34:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eW1Ac-0002QW-8Z for pgsql-general@arkaria.postgresql.org; Mon, 01 Jan 2018 14:34:30 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eW1Ab-0002QN-Oc for pgsql-general@lists.postgresql.org; Mon, 01 Jan 2018 14:34:30 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eW1AT-0001qf-1r for pgsql-general@lists.postgresql.org; Mon, 01 Jan 2018 14:34:28 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id A9E0221C1ADB for ; Mon, 1 Jan 2018 17:34:17 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.1.142] (ppp109-252-62-180.pppoe.spdop.ru [109.252.62.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 7D73D21C047B for ; Mon, 1 Jan 2018 17:34:17 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1514817257; bh=qN/hWSUsi/t+72VM0PF0eFVh/lWxtp5bfXxyKf85qQ8=; h=Subject:To:References:From:Date:In-Reply-To; b=P3TcLRXRSgTqCddiur95LhuSoJGQdcSusY7aspzMJSciRQSXwp4EV38trE/wJ98MF x3WR0PtuJ/cYFjHXfN1oLaRxOvIHdtULkCKRa/4/ldgfaJLVNaBABDTCp27JG4biWV 982oGW56myJkrVGt+LoT3iPecroZPvgxvWgn6L64= Subject: Re: Selecting a JSON object of arrays from a PostgreSQL table To: pgsql-general@lists.postgresql.org References: From: "Ivan E. Panchenko" Message-ID: Date: Mon, 1 Jan 2018 17:34:17 +0300 User-Agent: Mozilla/5.0 (X11; Linux i686; rv:52.0) Gecko/20100101 Thunderbird/52.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 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 timestamptz, >     msg text > ); > > Here I fill the table with a simple test data: > > INSERT INTO chat(gid, uid, created, msg) VALUES >     (10, 1, NOW() + interval '1 min', 'msg 1'), >     (10, 2, NOW() + interval '2 min', 'msg 2'), >     (10, 1, NOW() + interval '3 min', 'msg 3'), >     (10, 2, NOW() + interval '4 min', 'msg 4'), >     (10, 1, NOW() + interval '5 min', 'msg 5'), >     (10, 2, NOW() + interval '6 min', 'msg 6'), >     (20, 3, NOW() + interval '7 min', 'msg 7'), >     (20, 4, NOW() + interval '8 min', 'msg 8'), >     (20, 4, NOW() + interval '9 min', 'msg 9'); > > And can fetch the data by running the SELECT query: > > SELECT ARRAY_TO_JSON( >   COALESCE(ARRAY_AGG(ROW_TO_JSON(x)), >            array[]::json[])) FROM ( > SELECT >     gid, >     uid, >     EXTRACT(EPOCH FROM created)::int AS created, >     msg > FROM chat) x; > > which returns me a JSON-array: > > [{"gid":10,"uid":1,"created":1514813043,"msg":"msg 1"}, >  {"gid":10,"uid":2,"created":1514813103,"msg":"msg 2"}, >  {"gid":10,"uid":1,"created":1514813163,"msg":"msg 3"}, >  {"gid":10,"uid":2,"created":1514813223,"msg":"msg 4"}, >  {"gid":10,"uid":1,"created":1514813283,"msg":"msg 5"}, >  {"gid":10,"uid":2,"created":1514813343,"msg":"msg 6"}, >  {"gid":20,"uid":3,"created":1514813403,"msg":"msg 7"}, >  {"gid":20,"uid":4,"created":1514813463,"msg":"msg 8"}, >  {"gid":20,"uid":4,"created":1514813523,"msg":"msg 9"}] > > However I would like to use "gid" as JSON object properties and the > rest data as values in that object: > > {"10": [{"uid":1,"created":1514813043,"msg":"msg 1"}, >         {"uid":2,"created":1514813103,"msg":"msg 2"}, >         {"uid":1,"created":1514813163,"msg":"msg 3"}, >         {"uid":2,"created":1514813223,"msg":"msg 4"}, >         {"uid":1,"created":1514813283,"msg":"msg 5"}, >         {"uid":2,"created":1514813343,"msg":"msg 6"}], > >  "20": [{"uid":3,"created":1514813403,"msg":"msg 7"}, >         {"uid":4,"created":1514813463,"msg":"msg 8"}, >         {"uid":4,"created":1514813523,"msg":"msg 9"}]} > > Is that please doable by using the PostgreSQL JSON functions? > > Thank you > Alex