[英]PostgreSQL Json_build_object / json_agg add level of aggregation
I am using PostgreSQL 10.4, compiled by Visual C++ build 1800, 64-bit;我使用的是 PostgreSQL 10.4,由 Visual C++ build 1800,64 位编译;
The query below yields a resultset of 1 column with type JSON and multiple rows, I have a requirement that the query returns every row into an array (basically 1 row and 1 column) with for example json_agg().下面的查询产生一个 1 列的结果集,类型为 JSON 和多行,我要求查询将每一行返回到一个数组(基本上是 1 行和 1 列)中,例如 json_agg()。
Unfortunately if I put json_agg() around the json_build_object I am getting an error that its impossible:不幸的是,如果我将 json_agg() 放在 json_build_object 周围,我会收到一个错误,这是不可能的:
ERROR: aggregate function calls cannot be nested
LINE 28: '$values', json_agg(fv.*)
SELECT json_build_object(
'id', vl.id,
'id_form', vl.id_form,
'id_waardenlijst', vl.id_waardenlijst,
'$values', json_agg(fv.*)
) FROM var_list vl
LEFT JOIN testscheme.form_values fv
on fv.id_form_record = vl.id
GROUP BY vl.id, vl.id_form, vl.id_waardenlijst
How can I add another layer of aggregation that returns me 1 column and 1 row with an array of my desired objects?如何添加另一层聚合,返回 1 列和 1 行以及我想要的对象数组?
I hope I was clear!我希望我很清楚!
Basically, you need another level of aggregation:基本上,您需要另一个级别的聚合:
SELECT json_agg(js) js_final
FROM (
SELECT json_build_object(
'id', vl.id,
'id_form', vl.id_form,
'id_waardenlijst', vl.id_waardenlijst,
'$values', json_agg(fv.*)
) js
FROM var_list vl
LEFT JOIN testscheme.form_values fv ON on fv.id_form_record = vl.id
GROUP BY vl.id, vl.id_form, vl.id_waardenlijst
) t
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.