聚合 JSON 列值作为键的数组
Aggregate JSON arrays with column values as keys
我有一个场景,我试图聚合数据并将聚合的数据插入另一个 table,所有这些都是从函数内部进行的。数据被插入到另一个 table 作为数组和 JSON。我已经能够完美地聚合到数组中,但是我 运行 在尝试以我想要的方式将数据聚合到 JSON 中时遇到了一些麻烦。
基本上这是我正在汇总的数据示例:
id_1 | id_2 | cat_ids_array
------+------+---------------
201 | 4232 | {9,10,11,13}
201 | 4236 | {11}
201 | 4249 | {12}
201 | 4251 | {9,10}
202 | 4245 | {11}
202 | 4249 | {12}
202 | 4251 | {9,10}
202 | 4259 | {9}
203 | 4232 | {9,10,11,13}
203 | 4236 | {11}
203 | 4249 | {12}
203 | 4251 | {9,10}
203 | 4377 | {14}
204 | 4232 | {15,108}
204 | 4236 | {15}
205 | 4232 | {17,109}
205 | 4245 | {17}
205 | 4377 | {18}
206 | 4253 | {20}
当我使用 json_agg()
将 id_2
和 cat_ids_array
聚合成一个 JSON 字符串时,我得到的是:
id_1 | json_agg
------+----------------------------------
201 | [{"f1":4232,"f2":[9,10,11,13]}, +
| {"f1":4236,"f2":[11]}, +
| {"f1":4249,"f2":[12]}, +
| {"f1":4251,"f2":[9,10]}]
202 | [{"f1":4245,"f2":[11]}, +
| {"f1":4249,"f2":[12]}, +
| {"f1":4251,"f2":[9,10]}, +
| {"f1":4259,"f2":[9]}]
203 | [{"f1":4232,"f2":[9,10,11,13]}, +
| {"f1":4236,"f2":[11]}, +
| {"f1":4249,"f2":[12]}, +
| {"f1":4251,"f2":[9,10]} +
| {"f1":4377,"f2":[14]}]
204 | [{"f1":4232,"f2":[15,108]}, +
| {"f1":4236,"f2":[15]}]
205 | [{"f1":4232,"f2":[17,109]}, +
| {"f1":4245,"f2":[17]}, +
| {"f1":4377,"f2":[18]}]
206 | [{"f1":4253,"f2":[20]}]
这是我想要得到的:
id_1 | json_agg
------+-------------------------------------------------------------
201 | [{"4232":[9,10,11,13],"4236":[11],"4249":[12],"4251":[9,10]}]
202 | [{"4245":[11],"4249":[12],"4251":[9,10],"4259":[9]}]
203 | [{"4232":[9,10,11,13],"4236":[11],"4249":[12],"4251":[9,10],"4377":[14]}]
204 | [{"4232":[15,108],"4236":[15]}]
205 | [{"4232":[17,109],"4245":[17],"4377":[18]}]
206 | [{"4253":[20]}]
我想我将不得不进行某种字符串连接,但我不完全确定最好的方法。如前所述,我是在函数内部执行此操作的,因此我可以灵活地执行操作,因为我不仅限于 SELECT 语法魔法。
也有关系,我是运行 PostgreSQL 9.3.4,近期无法升级到9.4。
遗憾的是您不能升级,Postgres 9.4 有 jsonb
和 JSON 的许多新增功能。特别是 json_build_object()
非常适合您:
- Return multiple columns of the same row as JSON array of objects
差不多,但不完全是
虽然坚持使用 Postgres 9.3,但您可以从 hstore
获得帮助,以构建一个 hstore
值,其中 id_2
为键,cat_ids_array
为值:
hstore(id_2::text, cat_ids_array::text)
或者:
hstore(id_2::text, array_to_json(cat_ids_array)::text)
然后:
json_agg(hstore(id_2::text, array_to_json(cat_ids_array)::text))
但该数组未被识别为数组。一旦转换为 hstore
,它就是 Postgres 的文本字符串。有hstore_to_json_loose()
,但只识别boolean
和数值类型
解决方案
所以我最终像你预测的那样进行了字符串操作。有多种构造 json 字符串的方法。每个都或多或少快速/优雅:
format('{"%s":[%s]}', id_2::text, translate(cat_ids_array::text, '{}',''))::json
format('{"%s":%s}', id_2::text, to_json(cat_ids_array))::json
replace(replace(to_json((id_2, cat_ids_array))::text, 'f1":',''),',"f2', '')::json
我选择了第二个变体,似乎是最可靠的并且适用于其他数组类型而不是简单的 int[]
,这可能需要转义:
SELECT id_1
, json_agg(format('{"%s":%s}', id_2::text, to_json(cat_ids_array))::json)
FROM tbl
GROUP BY 1
ORDER BY 1;
结果如愿。
SQL Fiddle演示全部。
我有一个场景,我试图聚合数据并将聚合的数据插入另一个 table,所有这些都是从函数内部进行的。数据被插入到另一个 table 作为数组和 JSON。我已经能够完美地聚合到数组中,但是我 运行 在尝试以我想要的方式将数据聚合到 JSON 中时遇到了一些麻烦。
基本上这是我正在汇总的数据示例:
id_1 | id_2 | cat_ids_array
------+------+---------------
201 | 4232 | {9,10,11,13}
201 | 4236 | {11}
201 | 4249 | {12}
201 | 4251 | {9,10}
202 | 4245 | {11}
202 | 4249 | {12}
202 | 4251 | {9,10}
202 | 4259 | {9}
203 | 4232 | {9,10,11,13}
203 | 4236 | {11}
203 | 4249 | {12}
203 | 4251 | {9,10}
203 | 4377 | {14}
204 | 4232 | {15,108}
204 | 4236 | {15}
205 | 4232 | {17,109}
205 | 4245 | {17}
205 | 4377 | {18}
206 | 4253 | {20}
当我使用 json_agg()
将 id_2
和 cat_ids_array
聚合成一个 JSON 字符串时,我得到的是:
id_1 | json_agg
------+----------------------------------
201 | [{"f1":4232,"f2":[9,10,11,13]}, +
| {"f1":4236,"f2":[11]}, +
| {"f1":4249,"f2":[12]}, +
| {"f1":4251,"f2":[9,10]}]
202 | [{"f1":4245,"f2":[11]}, +
| {"f1":4249,"f2":[12]}, +
| {"f1":4251,"f2":[9,10]}, +
| {"f1":4259,"f2":[9]}]
203 | [{"f1":4232,"f2":[9,10,11,13]}, +
| {"f1":4236,"f2":[11]}, +
| {"f1":4249,"f2":[12]}, +
| {"f1":4251,"f2":[9,10]} +
| {"f1":4377,"f2":[14]}]
204 | [{"f1":4232,"f2":[15,108]}, +
| {"f1":4236,"f2":[15]}]
205 | [{"f1":4232,"f2":[17,109]}, +
| {"f1":4245,"f2":[17]}, +
| {"f1":4377,"f2":[18]}]
206 | [{"f1":4253,"f2":[20]}]
这是我想要得到的:
id_1 | json_agg
------+-------------------------------------------------------------
201 | [{"4232":[9,10,11,13],"4236":[11],"4249":[12],"4251":[9,10]}]
202 | [{"4245":[11],"4249":[12],"4251":[9,10],"4259":[9]}]
203 | [{"4232":[9,10,11,13],"4236":[11],"4249":[12],"4251":[9,10],"4377":[14]}]
204 | [{"4232":[15,108],"4236":[15]}]
205 | [{"4232":[17,109],"4245":[17],"4377":[18]}]
206 | [{"4253":[20]}]
我想我将不得不进行某种字符串连接,但我不完全确定最好的方法。如前所述,我是在函数内部执行此操作的,因此我可以灵活地执行操作,因为我不仅限于 SELECT 语法魔法。
也有关系,我是运行 PostgreSQL 9.3.4,近期无法升级到9.4。
遗憾的是您不能升级,Postgres 9.4 有 jsonb
和 JSON 的许多新增功能。特别是 json_build_object()
非常适合您:
- Return multiple columns of the same row as JSON array of objects
差不多,但不完全是
虽然坚持使用 Postgres 9.3,但您可以从 hstore
获得帮助,以构建一个 hstore
值,其中 id_2
为键,cat_ids_array
为值:
hstore(id_2::text, cat_ids_array::text)
或者:
hstore(id_2::text, array_to_json(cat_ids_array)::text)
然后:
json_agg(hstore(id_2::text, array_to_json(cat_ids_array)::text))
但该数组未被识别为数组。一旦转换为 hstore
,它就是 Postgres 的文本字符串。有hstore_to_json_loose()
,但只识别boolean
和数值类型
解决方案
所以我最终像你预测的那样进行了字符串操作。有多种构造 json 字符串的方法。每个都或多或少快速/优雅:
format('{"%s":[%s]}', id_2::text, translate(cat_ids_array::text, '{}',''))::json
format('{"%s":%s}', id_2::text, to_json(cat_ids_array))::json
replace(replace(to_json((id_2, cat_ids_array))::text, 'f1":',''),',"f2', '')::json
我选择了第二个变体,似乎是最可靠的并且适用于其他数组类型而不是简单的 int[]
,这可能需要转义:
SELECT id_1
, json_agg(format('{"%s":%s}', id_2::text, to_json(cat_ids_array))::json)
FROM tbl
GROUP BY 1
ORDER BY 1;
结果如愿。
SQL Fiddle演示全部。