尝试按顺序时间戳时,ARRAY_AGG被GROUP BY中断

时间:2018-01-02 10:38:10

标签: sql postgresql sql-order-by postgresql-json array-agg

我为我的问题准备了SQL Fiddle -

鉴于下表:

CREATE TABLE chat(
    gid integer,            /* game id */
    uid integer,            /* user id */
    created timestamptz,
    msg text
);

填写以下测试数据:

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');

我可以使用以下查询:

SELECT json_object_agg(
          gid, array_to_json(y)
       ) FROM (
       SELECT  gid,
          array_agg(
            json_build_object(
              'uid', uid,
              'created', EXTRACT(EPOCH FROM created)::int,
              'msg', msg)
          ) y
        FROM  chat 
        GROUP BY gid /*, created
        ORDER BY created ASC */
) x;

用于将表记录作为JSON对象获取,并将“gid”作为键和数组值:

{ "20" : [
    {"uid" : 3, "created" : 1514889490, "msg" : "msg 7"},
    {"uid" : 4, "created" : 1514889550, "msg" : "msg 8"},
    {"uid" : 4, "created" : 1514889610, "msg" : "msg 9"}
], 

"10" : [
    {"uid" : 1, "created" : 1514889130, "msg" : "msg 1"},
    {"uid" : 2, "created" : 1514889190, "msg" : "msg 2"},
    {"uid" : 1, "created" : 1514889250, "msg" : "msg 3"},
    {"uid" : 2, "created" : 1514889310, "msg" : "msg 4"},
    {"uid" : 1, "created" : 1514889370, "msg" : "msg 5"},
    {"uid" : 2, "created" : 1514889430, "msg" : "msg 6"}
] }

然而,我错过了一件小事而无法理解:

我需要通过“created”来命令数组。

因此,我将ORDER BY created ASC添加到上述查询中,并且还必须添加GROUP BY gid, created(您可以在上述查询中看到有问题的代码)。

这个打破然而array_agg并导致1个元素的数组(并且从我的自定义存储函数返回为jsonb时覆盖重复的“gid”键):

{ "10": [{ "uid":2, "created":1514889800, "msg":"msg 6" }],

  "20": [{ "uid":4, "created":1514889980, "msg":"msg 9" }] }

这里是否可以订购,或者我应该在我的JAVA应用程序中使用它?

1 个答案:

答案 0 :(得分:2)

ORDER BY电话中指定array_agg

SELECT json_object_agg(gid, array_to_json(y))
FROM (
  SELECT  gid,
    array_agg(
      json_build_object(
        'uid', uid,
        'created', EXTRACT(EPOCH FROM created)::int,
        'msg', msg
      ) ORDER BY created -- Order the elements in the resulting array
    ) AS y
  FROM chat 
  GROUP BY gid
) x;

返回(使用jq格式化):

{
  "10": [
    {
      "uid": 1,
      "created": 1514890093,
      "msg": "msg 1"
    },
    {
      "uid": 2,
      "created": 1514890153,
      "msg": "msg 2"
    },
    {
      "uid": 1,
      "created": 1514890213,
      "msg": "msg 3"
    },
    {
      "uid": 2,
      "created": 1514890273,
      "msg": "msg 4"
    },
    {
      "uid": 1,
      "created": 1514890333,
      "msg": "msg 5"
    },
    {
      "uid": 2,
      "created": 1514890393,
      "msg": "msg 6"
    }
  ],
  "20": [
    {
      "uid": 3,
      "created": 1514890453,
      "msg": "msg 7"
    },
    {
      "uid": 4,
      "created": 1514890513,
      "msg": "msg 8"
    },
    {
      "uid": 4,
      "created": 1514890573,
      "msg": "msg 9"
    }
  ]
}