Re: How to remove duplicates in an array and maintain the order?

From: Marcos Pegoraro <marcos(at)f10(dot)com(dot)br>
To: Shaozhong SHI <shishaozhong(at)gmail(dot)com>
Cc: pgsql-sql <pgsql-sql(at)lists(dot)postgresql(dot)org>
Subject: Re: How to remove duplicates in an array and maintain the order?
Date: 2023-07-08 15:50:54
Message-ID: CAB-JLwY+FKxi-SHM_HQXgYO6uxJu2bb1qgUqjNQyQBgnS9DPhQ@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-sql

>
> How to remove duplicated values in an array and maintain the order?
>
> select array_agg(unnest) filter (where row_number = 1) from (select
unnest, ordinality, row_number() over(partition by unnest order by
ordinality) from (select * from
unnest('{7,7,5,6,2,2,3,8,5,4,5}'::integer[]) with ordinality) x order by
ordinality) x

{7,5,6,2,3,8,4}

regards
Marcos

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Thomas Kellerer 2023-07-08 16:19:10 Re: How to remove duplicates in an array and maintain the order?
Previous Message David G. Johnston 2023-07-08 15:36:19 Re: How to remove duplicates in an array and maintain the order?