PostgreSQL 数组函数扩展

函数定义

drop function if exists array_repeat(bigint[]);
drop function if exists array_repeat(text[]);
drop function if exists array_different(bigint[],bigint[]);
drop function if exists array_different(text[],text[]);
drop function if exists array_same(bigint[],bigint[]);
drop function if exists array_same(text[],text[]);

/****************************************************************************************
    数组去除重复的值,去重后按升序排序
    select * from array_repeat(array[1,1,2,2,2,3])
    {1,2,3}
drop function if exists array_repeat(bigint[]);
drop function if exists array_repeat(text[]);
****************************************************************************************/
create or replace function array_repeat(bigint[])
  returns table(val bigint[])
as $$
    with first as(
		select val from unnest($1) as val
	),second as(
		select val from first group by val order by val
	)select array_agg(val) from second
$$ language sql strict immutable;

create or replace function array_repeat(text[])
  returns table(val text[])
as $$
    with first as(
		select val from unnest($1) as val
	),second as(
		select val from first group by val order by val
	)select array_agg(val) from second
$$ language sql strict immutable;

/****************************************************************************************
    求两个数组的差异部份,去除共同部份后按升序排序
    select * from array_different(array[1,2,3,4,5,6,7,8,9,10]::bigint[],array[5,6,7,8,9,10,11,12,13,14,15]::bigint[])
    {1,2,3,4,11,12,15,13,14}
drop function if exists array_different(bigint[],bigint[]);
drop function if exists array_different(text[],text[]);
****************************************************************************************/
create or replace function array_different(bigint[],bigint[])
  returns table(val bigint[])
as $$
    with first as(
		select val from unnest($1) as val
	),second as(
		select val from unnest($2) as val
	),different as(
		select unnest(array_agg(first.val) || array_agg(second.val)) as val
			from first full outer join second on first.val=second.val 
				where first.val is null or second.val is null order by val
	) select array_agg(val) from different where val is not null
$$ language sql strict immutable;

create or replace function array_different(text[],text[])
  returns table(val text[])
as $$
    with first as(
		select val from unnest($1) as val
	),second as(
		select val from unnest($2) as val
	),different as(
		select unnest(array_agg(first.val) || array_agg(second.val)) as val
			from first full outer join second on first.val=second.val 
				where first.val is null or second.val is null order by val
	) select array_agg(val) from different where val is not null
$$ language sql strict immutable;

/****************************************************************************************
    求两个数组的相同部份,去除差异部份后按升序排序
    select * from array_same(array[1,2,3,4,5,6,7,8,9,10]::bigint[],array[5,6,7,8,9,10,11,12,13,14,15]::bigint[])
    {5,6,7,8,9,10}
drop function if exists array_same(bigint[],bigint[]);
drop function if exists array_same(text[],text[]);
****************************************************************************************/
create or replace function array_same(bigint[],bigint[])
  returns table(val bigint[])
as $$
    with first as(
		select val from unnest($1) as val
	),second as(
		select val from unnest($2) as val
	),same as(
		select unnest(array_agg(first.val) || array_agg(second.val)) as val
			from first inner join second on first.val=second.val
	),merges as(
		 select val from same group by val order by val
	) select array_agg(val) from merges
$$ language sql strict immutable;


create or replace function array_same(text[],text[])
  returns table(val text[])
as $$
   with first as(
		select val from unnest($1) as val
	),second as(
		select val from unnest($2) as val
	),same as(
		select unnest(array_agg(first.val) || array_agg(second.val)) as val
			from first inner join second on first.val=second.val
	),merges as(
		 select val from same group by val order by val
	) select array_agg(val) from merges
$$ language sql strict immutable;

postgresql数组操作符等于和不等于要求两个数组的元素值和值的位置必须完全一致,因此下面的方法都返回false

select array[1,2,3]::bigint[]=array[3,2,1]::bigint[]
select array[1,2,3]::bigint[]<>array[3,2,1]::bigint[]

值和值的位置必须完全相同返回ture

select array[1,2,3]::bigint[]=array[1,2,3]::bigint[]
select array[1,2,3]::bigint[]<>array[1,2,3]::bigint[]

重要的事情说3遍,避免大家填坑
postgresql数组操作符等于和不等于要求元素值和位置必须完全相同
postgresql数组操作符等于和不等于要求元素值和位置必须完全相同
postgresql数组操作符等于和不等于要求元素值和位置必须完全相同

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kmblack1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值