如何聚合 JSONB PostgreSQL 10
How to aggregate JSONB PostgreSQL 10
我有 table ids 和 jsonb 列
1, "{"a" : {"per" : 0.45, "number" : 1}}"
1, "{"b" : {"per" : 3.13, "number" : 7}}"
2, "{"a" : {"per" : 14.73, "number" : 33}}"
2, "{"b" : {"per" : 85.27, "number" : 191}}"
3, "{"a" : {"per" : 40.63, "number" : 91}}"
4, "{"a" : {"per" : 1.79, "number" : 4}}"
4, "{"b" : {"per" : 2.68, "number" : 6}}"
需要按 id 对值进行分组并得到此结果
1, "{"a" : {"per" : 0.45, "number" : 1}, "b" : {"per" : 3.13, "number" : 7}}"
2, "{"a" : {"per" : 14.73, "number" : 33}, "b" : {"per" : 85.27, "number" : 191}}"
3, "{"a" : {"per" : 40.63, "number" : 91}}"
4, "{"a" : {"per" : 1.79, "number" : 4}, "b" : {"per" : 2.68, "number" : 6}}"
有什么办法可以实现吗?
没有内置聚合,但您可以轻松创建自己的聚合:
CREATE AGGREGATE jsonb_merge(jsonb)
(
SFUNC = jsonb_concat(jsonb, jsonb),
STYPE = jsonb
);
那么你可以这样做:
select id, jsonb_merge(the_jsonb_column)
from your_table
group by id
我有 table ids 和 jsonb 列
1, "{"a" : {"per" : 0.45, "number" : 1}}"
1, "{"b" : {"per" : 3.13, "number" : 7}}"
2, "{"a" : {"per" : 14.73, "number" : 33}}"
2, "{"b" : {"per" : 85.27, "number" : 191}}"
3, "{"a" : {"per" : 40.63, "number" : 91}}"
4, "{"a" : {"per" : 1.79, "number" : 4}}"
4, "{"b" : {"per" : 2.68, "number" : 6}}"
需要按 id 对值进行分组并得到此结果
1, "{"a" : {"per" : 0.45, "number" : 1}, "b" : {"per" : 3.13, "number" : 7}}"
2, "{"a" : {"per" : 14.73, "number" : 33}, "b" : {"per" : 85.27, "number" : 191}}"
3, "{"a" : {"per" : 40.63, "number" : 91}}"
4, "{"a" : {"per" : 1.79, "number" : 4}, "b" : {"per" : 2.68, "number" : 6}}"
有什么办法可以实现吗?
没有内置聚合,但您可以轻松创建自己的聚合:
CREATE AGGREGATE jsonb_merge(jsonb)
(
SFUNC = jsonb_concat(jsonb, jsonb),
STYPE = jsonb
);
那么你可以这样做:
select id, jsonb_merge(the_jsonb_column)
from your_table
group by id