public inbox for [email protected]
help / color / mirror / Atom feedHow to remove duplicates in an array and maintain the order?
4+ messages / 4 participants
[nested] [flat]
* How to remove duplicates in an array and maintain the order?
@ 2023-07-08 15:30 Shaozhong SHI <[email protected]>
2023-07-08 15:36 ` Re: How to remove duplicates in an array and maintain the order? David G. Johnston <[email protected]>
2023-07-08 15:50 ` Re: How to remove duplicates in an array and maintain the order? Marcos Pegoraro <[email protected]>
2023-07-08 16:19 ` Re: How to remove duplicates in an array and maintain the order? Thomas Kellerer <[email protected]>
0 siblings, 3 replies; 4+ messages in thread
From: Shaozhong SHI @ 2023-07-08 15:30 UTC (permalink / raw)
To: pgsql-sql <[email protected]>
How to remove duplicated values in an array and maintain the order?
Regards,
David
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: How to remove duplicates in an array and maintain the order?
2023-07-08 15:30 How to remove duplicates in an array and maintain the order? Shaozhong SHI <[email protected]>
@ 2023-07-08 15:36 ` David G. Johnston <[email protected]>
2 siblings, 0 replies; 4+ messages in thread
From: David G. Johnston @ 2023-07-08 15:36 UTC (permalink / raw)
To: Shaozhong SHI <[email protected]>; +Cc: pgsql-sql <[email protected]>
On Sat, Jul 8, 2023 at 8:30 AM Shaozhong SHI <[email protected]> wrote:
> How to remove duplicated values in an array and maintain the order?
>
Use a for loop in pl/pgsql.
David J.
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: How to remove duplicates in an array and maintain the order?
2023-07-08 15:30 How to remove duplicates in an array and maintain the order? Shaozhong SHI <[email protected]>
@ 2023-07-08 15:50 ` Marcos Pegoraro <[email protected]>
2 siblings, 0 replies; 4+ messages in thread
From: Marcos Pegoraro @ 2023-07-08 15:50 UTC (permalink / raw)
To: Shaozhong SHI <[email protected]>; +Cc: pgsql-sql <[email protected]>
>
> 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
^ permalink raw reply [nested|flat] 4+ messages in thread
* Re: How to remove duplicates in an array and maintain the order?
2023-07-08 15:30 How to remove duplicates in an array and maintain the order? Shaozhong SHI <[email protected]>
@ 2023-07-08 16:19 ` Thomas Kellerer <[email protected]>
2 siblings, 0 replies; 4+ messages in thread
From: Thomas Kellerer @ 2023-07-08 16:19 UTC (permalink / raw)
To: [email protected]
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
^ permalink raw reply [nested|flat] 4+ messages in thread
end of thread, other threads:[~2023-07-08 16:19 UTC | newest]
Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2023-07-08 15:30 How to remove duplicates in an array and maintain the order? Shaozhong SHI <[email protected]>
2023-07-08 15:36 ` David G. Johnston <[email protected]>
2023-07-08 15:50 ` Marcos Pegoraro <[email protected]>
2023-07-08 16:19 ` Thomas Kellerer <[email protected]>
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox