Postgres Jsonb 聚合

Postgres Jsonb aggregation

我正在尝试从 POSTGRES jsonb 列获得以下(需要结果)输出,但使用“jsonb_agg”函数没有得到想要的结果。 我浏览了这个 postgres 文档 https://www.postgresql.org/docs/12/functions-json.html,但运气不好。
还建议 json postgres 格式化相关内容的好资源。

City JColA JColB
NY [{"id":"ID1","name":"ID1_NAME","type":"ID1_TYPE","amount":20.12,"full_name":null},{"id":"ID2","name":"ID2_NAME","type":"ID2_TYPE","amount":11.55,"full_name":null},{"id":"ID1","name":"ID1_NAME","type":"ID1_TYPE","amount":5.45,"full_name":null}] [{"key":"key1","value":"1"},{"key":"key2","value":"2"},{"key":"key3","value":"3"}]
DC [{"id":"ID1","name":"ID1_NAME","type":"ID1_TYPE","amount":1.5,"full_name":null},{"id":"ID3","name":"ID3_NAME","type":"ID3_TYPE","amount":1.2,"full_name":null},{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":1,"full_name":null}] [{"key":"key1","value":"1"},{"key":"key1","value":"2"},{"key":"key1","value":"3"}]
DL [{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":1.5,"full_name":null},{"id":"ID2","name":"ID2_NAME","type":"ID2_TYPE","amount":1.2,"full_name":null},{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":1,"full_name":null}] [{"key":"key1","value":"2"},{"key":"key2","value":"2"},{"key":"key3","value":"4"}]
NY [{"id":"ID1","name":"ID1_NAME","type":"ID1_TYPE","amount":4.5,"full_name":null},{"id":"ID2","name":"ID2_NAME","type":"ID2_TYPE","amount":2.2,"full_name":null},{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":6,"full_name":null}] [{"key":"key4","value":"2"},{"key":"key2","value":"5"},{"key":"key2","value":"4"}]
DC [{"id":"ID3","name":"ID3_NAME","type":"ID3_TYPE","amount":2.5,"full_name":null},{"id":"ID3","name":"ID3_NAME","type":"ID3_TYPE","amount":2.2,"full_name":null},{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":2,"full_name":null}] [{"key":"key1","value":"2"},{"key":"key2","value":"2"},{"key":"key3","value":"4"}]

要求的结果

City AggJSonColA AggJsonColB
NY [{"id":"ID1","name":"ID1_NAME","type":"ID1_TYPE","amount":30.07,"full_name":null},{"id":"ID2","name":"ID2_NAME","type":"ID2_TYPE","amount":13.75,"full_name":null},{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":6,"full_name":null}] [{"key":"key1","value":"1"},{"key":"key2","value":"11"},{"key":"key3","value":"3"}, {"key":"key4","value":"2"}]
DC [{"id":"ID1","name":"ID1_NAME","type":"ID1_TYPE","amount":1.5,"full_name":null},{"id":"ID3","name":"ID3_NAME","type":"ID3_TYPE","amount":5.9,"full_name":null},{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":3,"full_name":null}] [{"key":"key1","value":"8"},{"key":"key2","value":"2"},{"key":"key3","value":"4"}]
DL [{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":1.5,"full_name":null},{"id":"ID2","name":"ID2_NAME","type":"ID2_TYPE","amount":1.2,"full_name":null},{"id":"ID4","name":"ID4_NAME","type":"ID4_TYPE","amount":1,"full_name":null}] [{"key":"key1","value":"2"},{"key":"key2","value":"2"},{"key":"key3","value":"4"}]

您必须聚合不同子选择中的 jColAjColB 值。

您必须取消嵌套 jColAjColB 数组(使用 json_array_elements),然后将结果扩展到字段中(使用 json_to_recordset 或简单的 ->> 运算符)。

拥有普通数据集后,您可以对按其他 jColA 属性分组的 jColA 列和 value JColB 分组的 amout 列求和 key.

在你之后,你必须通过 city.

加入子查询的结果

您可以连续使用 JSONB_POPULATE_RECORDSET() 并为每个 JSONB 列创建类型,然后 JSONB_BUILD_OBJECT() 组合键值对,同时汇总数值,例如

WITH t0 AS
(
 SELECT city, 
        (JSONB_POPULATE_RECORDSET(NULL::jstA,JColA)).*,
        (JSONB_POPULATE_RECORDSET(NULL::jstB,JColB)).*
   FROM t
), tA AS
(
 SELECT city,
        JSONB_BUILD_OBJECT('id',id,'name',name,'type',type,'amount',SUM(amount),'full_name',full_name) AS jsA
   FROM t0
  GROUP BY city, id, name, type, full_name
  ORDER BY id
), tB AS
(
 SELECT city,
        JSONB_BUILD_OBJECT('key',key,'value',SUM(value)) AS jsB
   FROM t0
  GROUP BY city, key
  ORDER BY key
)
SELECT *
  FROM (SELECT city, JSONB_PRETTY(JSONB_AGG(jsA)) AS AggJsonColA FROM tA GROUP BY city) AS tA
  JOIN (SELECT city, JSONB_PRETTY(JSONB_AGG(jsB)) AS AggJsonColB FROM tB GROUP BY city) AS tB
    ON tB.city=tA.city

Demo