在PostgreSQL 9.4中,我有一个这样的表:
id | array_json
---+----------------------------
1 | [{"type": "single", "field_id": 9},
| {"type": "range", "field_id": 2}, ...]
|
2 | [{"type": "single", "field_id": 10},
| {"type": "range", "field_id": 2}, ...]
...
我希望得到所有表格中array_json列中所有 field_id 值的交集。
| field_id intersection
+-------
| 2
我的意思是:
1。映射第一行的field_id值:[9,2]
2。映射第二行的field_id值:[10,2]
n。映射 n 的field_id值...
...
最后。获取所有行的交集:[2](假设该表只有两行)
有人可以告诉我这是怎么做到的吗?
非常感谢提前
答案 0 :(得分:4)
您需要一个聚合来交叉连续行的数组:
create or replace function array_intersect(anyarray, anyarray)
returns anyarray language sql
as $$
select
case
when $1 is null then $2
when $2 is null then $1
else
array(
select unnest($1)
intersect
select unnest($2))
end;
$$;
create aggregate array_intersect_agg (anyarray)
(
sfunc = array_intersect,
stype = anyarray
);
使用jsonb_array_elements()
和array_agg()
以整数数组的形式检索field_ids
:
select id, array_agg(field_id) field_ids
from (
select id, (e->>'field_id')::int field_id
from a_table, jsonb_array_elements(array_json) e
) sub
group by 1
order by 1;
id | field_ids
----+-----------
1 | {9,2}
2 | {10,2}
(2 rows)
使用定义的交叉点聚合与所有行的数组相交:
select array_intersect_agg(field_ids)
from (
select id, array_agg(field_id) field_ids
from (
select id, (e->>'field_id')::int field_id
from a_table, jsonb_array_elements(array_json) e
) sub
group by 1
order by 1
) sub;
array_intersect_agg
---------------------
{2}
(1 row)