与 jsonb_path_query_array 不同

问题描述 投票:0回答:1

在 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]

所以,基本上我需要一个不同的东西,但在处理数组内部。
有没有办法用一些我没有想到的参数来做到这一点?


sql arrays json postgresql jsonb
1个回答
0
投票

您仍然需要一个独特的专栏来执行我的方法

架构(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}');


查询#1

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;


id12
数组_agg
类型1,类型2
类型4,类型3

在 DB Fiddle 上查看

© www.soinside.com 2019 - 2024. All rights reserved.