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

Поиск
Список
Период
Сортировка
От Thomas Kellerer
Тема Re: How to remove duplicates in an array and maintain the order?
Дата
Msg-id 320adc31-e193-14b9-e9b9-80625099b401@gmx.net
обсуждение исходный текст
Ответ на How to remove duplicates in an array and maintain the order?  (Shaozhong SHI <shishaozhong@gmail.com>)
Список pgsql-sql

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





В списке pgsql-sql по дате отправления:

Предыдущее
От: Marcos Pegoraro
Дата:
Сообщение: Re: How to remove duplicates in an array and maintain the order?
Следующее
От: Shaozhong SHI
Дата:
Сообщение: Finding out types of error