public inbox for [email protected]  
help / color / mirror / Atom feed
From: Thomas Kellerer <[email protected]>
To: [email protected]
Subject: Re: How to remove duplicates in an array and maintain the order?
Date: Sat, 8 Jul 2023 18:19:10 +0200
Message-ID: <[email protected]> (raw)
In-Reply-To: <CA+i5JwbwFsa2nHM+MG4mXinxx_fEzQGPrpUcz=T8yGDWPQrmnw@mail.gmail.com>
References: <CA+i5JwbwFsa2nHM+MG4mXinxx_fEzQGPrpUcz=T8yGDWPQrmnw@mail.gmail.com>



Shaozhong SHI schrieb am 08.07.2023 um 17:30:
> How to remove duplicated values in an array and maintain the order?

You can use distinct on ()

select array_agg(val order by idx)
from (
   select distinct on (val) val, idx
   from unnest(array[7,7,5,6,2,2,3,8,7,5,4,1,5]) with ordinality as t(val,idx)
   order by val, idx
) x


This picks the first occurrance of each element.
If you want to get the last occurrance of each value use "order by val, idx desc" in the inner select








view thread (4+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: How to remove duplicates in an array and maintain the order?
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox