数组重叠数 [英] Array overlap count

查看:44
本文介绍了数组重叠数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何计算作为参数传递的数组和类型为array的列之间的重叠计数.

How can I calculate the overlap count between an array passed as argument and a column of type array.

 SELECT id1, overlap_count(ary, '{32,56,72,...,996}') FROM tbl

它在ary字段中检查有多少元素与作为参数传递的数组匹配.

where it checks the ary field for how many of the elements match with the array passed as argument.

我的问题有两个.我的表是这样的:ID1,ID2,ary我想尽可能使用多维数组(每个数组可以是任意维),但我怀疑它会更复杂或更慢.

My question is two fold. My table is something like this : ID1,ID2,ary I want to use multidimensional arrays if possible (where every array can be any dimensions), but suspect it would be more complex or very slow.

如果不是这样,我将不得不使用具有一维数组的重复记录,像这样:

if not I would have to use duplicate records with one-dim array, like this :

  | ID1  | ID2 |  ary   |
  | 5    | 7   |  {...} |
  | 43   | 611 |  {...} |
  | 5    | 7   |  {...} |

推荐答案

这是作业的简单功能.

create or replace function overlap_count(arr_a integer[], arr_b integer[]) 
returns integer language sql immutable as
$$
 select count(*)::integer from unnest(arr_a) a inner join unnest(arr_b) b on a = b;
$$;

这篇关于数组重叠数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆