Sql 在postgres中将jsonb转换为正则数组
我有下表:Sql 在postgres中将jsonb转换为正则数组,sql,postgresql,jsonb,Sql,Postgresql,Jsonb,我有下表: "Id"| "Data" ====+================================================ 1 | { "emp": [ | {"id": "a1", "otherdata": "other"}, | {"id": "a2", "otherdata": "other"} | ] | } ----+-----
"Id"| "Data"
====+================================================
1 | { "emp": [
| {"id": "a1", "otherdata": "other"},
| {"id": "a2", "otherdata": "other"}
| ]
| }
----+------------------------------------------------
2 | { "emp": [
| {"id": "b1", "otherdata": "other"},
| {"id": "b2", "otherdata": "other"}
| ]
| }
-----------------------------------------------------
其中“数据”是jsonb
我需要创建此类型的临时表:
"Id"| "Emp"
====+=============
1 | {"a1", "a2"}
----+-------------
2 | {"b1", "b2"}
如何执行此操作?使用
jsonb_to_记录集
将数组值提取到行中,对它们进行分组,然后使用array_to_json
返回数组
SELECT a.id, array_to_json(array_agg(b.id)) AS emp
FROM mytable a
CROSS JOIN jsonb_to_recordset(a.data->'emp') AS b(id text)
GROUP BY a.id;
看