Search Postgresql Archives

Re: how to sort an array and remove duplicate in plpgsql

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxx [mailto:pgsql-general-owner@xxxxxxxxxxxxxx] On Behalf Of David Gagnon
Sent: 26 February 2007 14:16
To: pgsql-general@xxxxxxxxxxxxxx
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. 

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux