gpt4 book ai didi

json - Postgres : How to achieve json_agg like functionality on json_object_keys

转载 作者:行者123 更新时间:2023-11-29 11:50:13 25 4
gpt4 key购买 nike

如果我想在单个查询中获取聚合在 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

最佳答案

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

关于json - Postgres : How to achieve json_agg like functionality on json_object_keys,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25790546/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com