postgres:如何在json

编程入门 行业动态 更新时间:2024-10-24 04:35:03
postgres:如何在json_object_keys上实现类似json_agg的功能(postgres : How to achieve json_agg like functionality on json_object_keys)

如果我想在单个查询中获取聚合在json数组中的json对象的键列表。 这是我正在尝试的,它给了我一个错误:

Postgres版本:9.3

postgres=# create temporary table t_test ( postgres(# id integer, postgres(# options json postgres(# ); CREATE TABLE postgres=# insert into t_test values (1, '{"x": 1, "y": 2}'); INSERT 0 1 postgres=# select * from t_test ; id | options ----+------------------ 1 | {"x": 1, "y": 2} (1 row) postgres=# select json_object_keys(options) from t_test ; json_object_keys ------------------ x y (2 rows) postgres=# select json_agg(json_object_keys(options)) from t_test ; ERROR: set-valued function called in context that cannot accept a set

If I want to get the list of json object's keys aggregated in a json array in a single query. Here is what I am trying and it gives me an error:

Postgres version : 9.3

postgres=# create temporary table t_test ( postgres(# id integer, postgres(# options json postgres(# ); CREATE TABLE postgres=# insert into t_test values (1, '{"x": 1, "y": 2}'); INSERT 0 1 postgres=# select * from t_test ; id | options ----+------------------ 1 | {"x": 1, "y": 2} (1 row) postgres=# select json_object_keys(options) from t_test ; json_object_keys ------------------ x y (2 rows) postgres=# select json_agg(json_object_keys(options)) from t_test ; ERROR: set-valued function called in context that cannot accept a set

最满意答案

select json_agg(o) from ( select json_object_keys(options) as o from t_test ) s ; json_agg ------------ ["x", "y"] select json_agg(o) from ( select json_object_keys(options) as o from t_test ) s ; json_agg ------------ ["x", "y"]

更多推荐

本文发布于:2023-07-14 20:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107235.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   postgres   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!