Postgres Jsonb 聚合

Posted

技术标签:

【中文标题】Postgres Jsonb 聚合【英文标题】:Postgres Jsonb aggregation 【发布时间】:2021-11-03 11:35:50 【问题描述】:

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

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"]

【问题讨论】:

【参考方案1】:

您可以连续使用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

【讨论】:

【参考方案2】:

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

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

拥有纯数据集后,您可以将jColA 中的amout 列按其他jColA 属性和value JColB 分组按key 分组。

在你加入子查询的结果之后你必须通过city

【讨论】:

以上是关于Postgres Jsonb 聚合的主要内容,如果未能解决你的问题,请参考以下文章

展平来自 JSONB 字段的聚合键/值对?

Postgres:查询多个jsonb字段

如何在 postgres 中对 jsonb 值求和?

Postgres 数组列与 JSONB 列

弹簧靴 | JSONB Postgres |异常:无法加载类 [jsonb]

Postgres 合并为空 JSONB 数组