有关json_to_recordset的文档,请参见 https://www .postgresql.org/docs/9.6/static/functions-json.html ,对于交叉连接,请参见以下 https: //www.reddit.com/r/PostgreSQL/comments/2u6ah3/how_to_use_json_to_recordset_on_json_stored_in_a/co6hr65/ 编辑:正如您在更新中说的那样,您使用的是jsonb字段,因此无需使用json_to_recordset,而只需使用jsonb_to_recordset I have data stored in JSON arrays for each row in a PostgreSQL table like so:id data-------------------------------------------------1 [{"a": 1, "b": 2}, {"a": 3, "b":2}]2 [{"a": 5, "b": 8}, {"a": 9, "b":0}]How can I get the sum of specific keys in the recordsets grouped by id?Here is an example of the desired result set:id a b-------------------------------------------------1 4 42 14 8Update:I should also mention that the data column is a jsonb data type. 解决方案 select id, sum(a), sum(b) from jsontable j CROSS JOIN LATERAL json_to_recordset(j.data) as x(a integer, b integer)group by idhere you can test the query or fiddle with it http://rextester.com/ZTCY82930For the documentation on json_to_recordset, see this https://www.postgresql.org/docs/9.6/static/functions-json.htmland for the cross-join thing, see this https://www.reddit.com/r/PostgreSQL/comments/2u6ah3/how_to_use_json_to_recordset_on_json_stored_in_a/co6hr65/Edit: As you say in your update, you use a jsonb field, so instead of using json_to_recordset, just use jsonb_to_recordset 这篇关于PostgreSQL按行汇总JSON记录集键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-30 05:38