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

From: "Chris Coleman" <ChristopherC(at)eurocom(dot)co(dot)uk>
To: "David Gagnon" <david(dot)gagnon(at)cesart(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: how to sort an array and remove duplicate in plpgsql
Date: 2007-02-26 15:25:56
Message-ID: 538A2AED987D2F458E7FDDAEE5E1E13525021E@secure.eurocom.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of David Gagnon
Sent: 26 February 2007 14:16
To: pgsql-general(at)postgresql(dot)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.

Browse pgsql-general by date

  From Date Subject
Next Message A. Kretschmer 2007-02-26 15:31:03 Re: Querying all months even if don't exist
Previous Message Robert Fitzpatrick 2007-02-26 15:10:45 Querying all months even if don't exist