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
|