在 pgsql 中遇到
jsonb_path_query_array
的问题。select jsonb_path_query_array(public.table.column, '$.Idents.type' ) from public.table;
并获得如下输出:
[1] [Type1, Type1, Type1, Type2, Type2, Type2]
[2] [Type3, Type3, Type3, Type4, Type4]
有没有办法得到这样的东西?
[1] [Type1, Type2]
[2] [Type3, Type4]
所以,基本上我需要一个不同的东西,但在处理数组内部。有没有办法用一些我没有想到的参数来做到这一点?
您仍然需要一个独特的专栏来执行我的方法
架构(PostgreSQL v14)
create table public.example(id int,test jsonb);
insert into public.example (id, test) values (1, '{"Idents": [{"id": "id0", "type": "Type1"}, {"id": "id1", "type": "Type1"}, {"id": "id2", "type": "Type2"}, {"id": "id3", "type": "Type2"}], "val0": 1725397140, "val1": -10800}');
insert into public.example (id,test) values (2,'{"Idents": [{"id": "id4", "type": "Type3"}, {"id": "id5", "type": "Type3"}, {"id": "id6", "type": "Type4"}, {"id": "id6", "type": "Type4"}], "val0": 1725397140, "val1": -10800}');
WITH CTE AS (select DISTINCT id ,jsonb_path_query(public.example.test, '$.Idents.type' ) val
from public.example)
SELECT id, array_agg(val)
FROM CTE
GROUP BY id
ORDER BY id;
数组_agg | |
---|---|
类型1,类型2 | |
类型4,类型3 |