Re: sort a referenced list

Lists: pgsql-general
From: Matthew Peter <survivedsushi(at)yahoo(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: pl/pgsql uniq varchar[] sort?
Date: 2006-04-03 08:53:26
Message-ID: 20060403085327.16781.qmail@web35214.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general


Hello list. I'm trying to get a unique, sorted varchar array in pl/pgsql. Essentially a "group by" and "order by"'d varchar[].

Anyone got any ideas or point me in the right direction? Thanks.




---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC and save big.


From: "Merlin Moncure" <mmoncure(at)gmail(dot)com>
To: "Matthew Peter" <survivedsushi(at)yahoo(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: pl/pgsql uniq varchar[] sort?
Date: 2006-04-03 15:10:22
Message-ID: b42b73150604030810r3716ff33pe20ddc9169267176@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On 4/3/06, Matthew Peter <survivedsushi(at)yahoo(dot)com> wrote:
>
> Hello list. I'm trying to get a unique, sorted varchar array in pl/pgsql.
> Essentially a "group by" and "order by"'d varchar[].
>
> Anyone got any ideas or point me in the right direction? Thanks.

If your data is not an array type coming off the table but you want it
to end up that way, check out array_accum at
http://www.postgresql.org/docs/8.1/static/xaggr.html. All you have to
do is order the data going into the aggregate:

select array_accum(d) from
(
select d from t order by...
)

if your data is starting off as an array type, you have a few options.
you might get the most milage out of a pl/perl procedure to sort the
type. If the arrays are small and you absolutely had to do it in
plpgsql you could copy the values into a temp table, sort it via
query, and resinsert into an array using the above technique.

merlin


From: Matthew Peter <survivedsushi(at)yahoo(dot)com>
To: Merlin Moncure <mmoncure(at)gmail(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: sort a referenced list
Date: 2006-04-04 07:10:00
Message-ID: 20060404071000.5103.qmail@web35207.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

I'll help clarify exactly what I am trying to accomplish.

What I'm trying to do is create a function that accepts a list then sorts and groups the values (like in sql)... I will have an unique list I can convert it to an array later or leave it a list

I'd like to keep it simple and manipulate the argument like...

select $1 as list
group by list
order by list asc;

Which doesn't work, but that is the functionality I need if possible.


---------------------------------
New Yahoo! Messenger with Voice. Call regular phones from your PC for low, low rates.


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Matthew Peter <survivedsushi(at)yahoo(dot)com>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: sort a referenced list
Date: 2006-04-04 13:29:13
Message-ID: 18312.1144157353@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Matthew Peter <survivedsushi(at)yahoo(dot)com> writes:
> What I'm trying to do is create a function that accepts a list then sorts and groups the values (like in sql)... I will have an unique list I can convert it to an array later or leave it a list

There is no "list" data structure in SQL. There are tables, and there
are arrays, but it's not especially easy to pass an arbitrary table
value to a function. So you almost certainly need to define your
problem as "create a function that accepts an array then ...".

regards, tom lane


From: Matthew Peter <survivedsushi(at)yahoo(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: sort a referenced list
Date: 2006-04-04 19:22:59
Message-ID: 20060404192259.66257.qmail@web35208.mail.mud.yahoo.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote: Matthew Peter writes:
> What I'm trying to do is create a function that accepts a list then sorts and groups the values (like in sql)... I will have an unique list I can convert it to an array later or leave it a list

There is no "list" data structure in SQL. There are tables, and there
are arrays, but it's not especially easy to pass an arbitrary table
value to a function. So you almost certainly need to define your
problem as "create a function that accepts an array then ...".

regards, tom lane

Originally I wanted to pass in text or varchar array, group it, sort it then do as I willed with it, but I couldn't figure out how. I saw some functions in the contrib for doing these operations, yet only with integer arrays.

I could always try converting the array_sort, array_uniq int[] C function to accept text[], but I decided to ask and see if there was a simplier/standard way first since I don't know C all that well.

Thanks


---------------------------------
Blab-away for as little as 1ยข/min. Make PC-to-Phone Calls using Yahoo! Messenger with Voice.


From: Jim Nasby <jnasby(at)pervasive(dot)com>
To: Matthew Peter <survivedsushi(at)yahoo(dot)com>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Merlin Moncure <mmoncure(at)gmail(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: sort a referenced list
Date: 2006-04-06 20:48:37
Message-ID: BF269696-FB8C-4B2A-98E2-B0184965CE61@pervasive.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-general

On Apr 4, 2006, at 3:22 PM, Matthew Peter wrote:
> Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote: Matthew Peter writes:
> > What I'm trying to do is create a function that accepts a list
> then sorts and groups the values (like in sql)... I will have an
> unique list I can convert it to an array later or leave it a list
>
> There is no "list" data structure in SQL. There are tables, and there
> are arrays, but it's not especially easy to pass an arbitrary table
> value to a function. So you almost certainly need to define your
> problem as "create a function that accepts an array then ...".
>
> regards, tom lane
>
> Originally I wanted to pass in text or varchar array, group it,
> sort it then do as I willed with it, but I couldn't figure out how.
> I saw some functions in the contrib for doing these operations, yet
> only with integer arrays.
>
> I could always try converting the array_sort, array_uniq int[] C
> function to accept text[], but I decided to ask and see if there
> was a simplier/standard way first since I don't know C all that well.

Sadly there's very little in the way of array operators in
PostgreSQL, so your best bet would be to modify what's in contrib,
preferably allowing it to work with any data type. I'm guessing that
the int array stuff is in contrib and not in the backend because
we're waiting for someone to make it work for all types...
--
Jim C. Nasby, Sr. Engineering Consultant jnasby(at)pervasive(dot)com
Pervasive Software http://pervasive.com work: 512-231-6117
vcard: http://jim.nasby.net/pervasive.vcf cell: 512-569-9461