Re: how to sort an array and remove duplicate in plpgsql - Mailing list pgsql-general

From Chris Coleman
Subject Re: how to sort an array and remove duplicate in plpgsql
Date
Msg-id 538A2AED987D2F458E7FDDAEE5E1E13525021E@secure.eurocom.me.uk
Whole thread Raw
In response to how to sort an array and remove duplicate in plpgsql  ("David Gagnon" <david.gagnon@cesart.com>)
List pgsql-general
Hi,
 
I'm no postgres guru and am not sure if this is the accepted way or not, but:
 
CREATE OR REPLACE FUNCTION explode_array(in_array anyarray)
RETURNS SETOF anyelement AS
$BODY$
    SELECT ($1)[s] FROM generate_series(1, array_upper($1, 1)) AS s;
$BODY$
LANGUAGE 'sql' IMMUTABLE;
ALTER FUNCTION explode_array(in_array anyarray) OWNER TO postgres;
Then you could do:
 
SELECT DISTINCT explode_array(ARRAY[1, 2, 5, 3, 1, 2]) AS data ORDER BY data
 
==>
 
1,
2,
3,
5
 
Hope that helps,
 
Cheers
Chris


From: pgsql-general-owner@postgresql.org [mailto:pgsql-general-owner@postgresql.org] On Behalf Of David Gagnon
Sent: 26 February 2007 14:16
To: pgsql-general@postgresql.org
Subject: [GENERAL] how to sort an array and remove duplicate in plpgsql

Hi all,

  I’m messing up with this problem for a while and I searched the web without success.  I have an array of timestamp and I needed sorted and I need to remove duplicate value.  The Select statement offers the SORT BY and UNIQUE that may help me but so far I didn’t find the way to plug my array variable into the select and get back the sorted array in return.

Any help or clue will be really appreciated!

Regards

David

 

 




_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

This e-mail is confidential and may be read only by the intended recipient.
If you are not the intended recipient, please do not forward, copy or take
any action based on it and, in addition, please delete this email and
inform the sender.
We cannot be sure that this e-mail or its attachments are free from
viruses.  In keeping with good computing practice, please ensure that
you take adequate steps to check for any viruses.  Before replying
or sending any email to us, please consider that the internet is inherently
insecure and is an inappropriate medium for certain kinds of information.
We reserve the right to access and read all e-mails and attachments
entering or leaving our systems.

Registered office: Eurocom House, Ashbourne Road, Derby DE22 4NB Company number: 01574696. 

pgsql-general by date:

Previous
From: Robert Fitzpatrick
Date:
Subject: Querying all months even if don't exist
Next
From: "A. Kretschmer"
Date:
Subject: Re: Querying all months even if don't exist