数组 – 如何使用postgres获取不同的数组元素?
发布时间:2020-12-13 15:50:00 所属栏目:百科 来源:网络整理
导读:我在postgres中有一个重复值的数组.例如: SELECT cardinality(string_to_array('1,2,3,4,4',',')::int[]) as foo= "foo"="5" 我想获得独特的元素,例如: SELECT cardinality(uniq(string_to_array('1,')::int[])) as foo= -- No function matches the given
我在postgres中有一个重复值的数组.例如:
SELECT cardinality(string_to_array('1,2,3,4,4',',')::int[]) as foo => "foo"=>"5" 我想获得独特的元素,例如: SELECT cardinality(uniq(string_to_array('1,')::int[])) as foo => -- No function matches the given name and argument types. You might need to add explicit type casts. 我可以在不使用UNNEST的情况下获得postgres中数组的唯一元素吗? 解决方法
对于整数数组,使用
intarray extension:
create extension if not exists intarray; SELECT cardinality(uniq(string_to_array('1,')::int[])) as foo 或功能 create or replace function public.array_unique(arr anyarray) returns anyarray language sql as $function$ select array_agg(distinct a) from ( select unnest(arr) a ) alias $function$; 对于任何阵列. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |