pg.ddx.io  pgsql-admin@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Return last value in of array (PostgreSQL 7.2.1)
6+ messages / 3 participants
[nested] [flat]

* Return last value in of array (PostgreSQL 7.2.1)
@ 2003-09-22 20:21 Erwin Brandstetter <Brsaz@gmzx.at>
  2003-09-24 00:46 ` Re: Return last value in of array (PostgreSQL 7.2.1) Steve Crawford <scrawford@pinpointresearch.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Erwin Brandstetter @ 2003-09-22 20:21 UTC (permalink / raw)
  To: pgsql-admin

Hi Newsgroup!

I can't believe I have found a good solution here. I want to return the 
last value of an array with an SQL statement, while I do not know, how many 
items the array holds (u can never be sure of that since u can redim an 
array in PostgreSQL whenever u like..).

That is what i came up with:


select my_array_field[substr(array_dims(my_array_field), 4, 1)]
from my_table;


But this only works, as long as the upper boundary is from 1 to 9 (one 
digit). Besides it looks ugly and will perform likewise.

Did I miss something here?
Is there any function returning more sensible data than array_dims 
(returning something like '[1:4]')?

Grateful for any hints.
Regards
Erwin Brandstetter

-- 
no z in my mail.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Return last value in of array (PostgreSQL 7.2.1)
  2003-09-22 20:21 Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
@ 2003-09-24 00:46 ` Steve Crawford <scrawford@pinpointresearch.com>
  2003-09-29 16:54   ` Re: Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  0 siblings, 1 reply; 6+ messages in thread

From: Steve Crawford @ 2003-09-24 00:46 UTC (permalink / raw)
  To: Erwin Brandstetter <Brsaz@gmzx.at>; pgsql-admin

> I can't believe I have found a good solution here. I want to return
> the last value of an array with an SQL statement...
> That is what i came up with:
>
> select my_array_field[substr(array_dims(my_array_field), 4, 1)]
> from my_table;
>
> But this only works, as long as the upper boundary is from 1 to 9
> (one digit). Besides it looks ugly and will perform likewise.
>
> Did I miss something here?
> Is there any function returning more sensible data than array_dims
> (returning something like '[1:4]')?

IIRC I heard talk of new max and min array dimension functions in 
upcoming releases. I'm tired so there may be better or more elegant 
solutions but this one works (r is the array):

r[rtrim(substring(array_dims(r), position(':' in 
array_dims(r))+1),']')::int]

Cheers,
Steve




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Return last value in of array (PostgreSQL 7.2.1)
  2003-09-22 20:21 Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  2003-09-24 00:46 ` Re: Return last value in of array (PostgreSQL 7.2.1) Steve Crawford <scrawford@pinpointresearch.com>
@ 2003-09-29 16:54   ` Erwin Brandstetter <Brsaz@gmzx.at>
  2003-09-29 19:03     ` Re: Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  2003-10-06 02:51     ` Re: Return last value in of array (PostgreSQL 7.2.1) Joe Conway <mail@joeconway.com>
  0 siblings, 2 replies; 6+ messages in thread

From: Erwin Brandstetter @ 2003-09-29 16:54 UTC (permalink / raw)
  To: pgsql-admin

scrawford@pinpointresearch.com (Steve Crawford) wrote in
news:200309231746.13757.scrawford@pinpointresearch.com: 

> IIRC I heard talk of new max and min array dimension functions in 
> upcoming releases. I'm tired so there may be better or more elegant 
> solutions but this one works (r is the array):
> 
> r[rtrim(substring(array_dims(r), position(':' in 
> array_dims(r))+1),']')::int]

This works and is an improvement to my previous version as it allows for 
upper boundaries with any number of digits. Thank you!

Still the solution looks ugly and will perform likewise. PostgreSQL 
urgently needs improvment on array handling. Hopefully new versions take 
care of that (haven't tested 7.3+).

BTW: Have been away for some days, so my reply is late, sorry.


Regards
Erwin Brandstetter

-- 
no z in my mail.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Return last value in of array (PostgreSQL 7.2.1)
  2003-09-22 20:21 Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  2003-09-24 00:46 ` Re: Return last value in of array (PostgreSQL 7.2.1) Steve Crawford <scrawford@pinpointresearch.com>
  2003-09-29 16:54   ` Re: Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
@ 2003-09-29 19:03     ` Erwin Brandstetter <Brsaz@gmzx.at>
  1 sibling, 0 replies; 6+ messages in thread

From: Erwin Brandstetter @ 2003-09-29 19:03 UTC (permalink / raw)
  To: pgsql-admin

Not that it makes much of a difference, but i guess the following code 
might work slightly better:

r[(substring(array_dims(r), strpos(array_dims(r), ':') + 1, 
(length(array_dims(r)) - strpos(array_dims(r), ':')) - 1))::int]


Regards
Erwin Brandstetter
-- 
no z in my mail.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Return last value in of array (PostgreSQL 7.2.1)
  2003-09-22 20:21 Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  2003-09-24 00:46 ` Re: Return last value in of array (PostgreSQL 7.2.1) Steve Crawford <scrawford@pinpointresearch.com>
  2003-09-29 16:54   ` Re: Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
@ 2003-10-06 02:51     ` Joe Conway <mail@joeconway.com>
  2003-10-06 22:55       ` Re: Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  1 sibling, 1 reply; 6+ messages in thread

From: Joe Conway @ 2003-10-06 02:51 UTC (permalink / raw)
  To: Erwin Brandstetter <Brsaz@gmzx.at>; +Cc: pgsql-admin

Erwin Brandstetter wrote:
> Still the solution looks ugly and will perform likewise. PostgreSQL 
> urgently needs improvment on array handling. Hopefully new versions take 
> care of that (haven't tested 7.3+).

7.4 array functionality is significantly improved. Still room for more 
improvements, but well worth a look if you need to make use of arrays. 
See the following (not sure these urls will survive wrapping):

http://developer.postgresql.org/docs/postgres/sql-expressions.html#SQL-SYNTAX-ARRAY-CONSTRUCTORS
http://developer.postgresql.org/docs/postgres/arrays.html
http://developer.postgresql.org/docs/postgres/functions-array.html
http://developer.postgresql.org/docs/postgres/functions-comparisons.html

HTH,

Joe




^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Return last value in of array (PostgreSQL 7.2.1)
  2003-09-22 20:21 Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  2003-09-24 00:46 ` Re: Return last value in of array (PostgreSQL 7.2.1) Steve Crawford <scrawford@pinpointresearch.com>
  2003-09-29 16:54   ` Re: Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
  2003-10-06 02:51     ` Re: Return last value in of array (PostgreSQL 7.2.1) Joe Conway <mail@joeconway.com>
@ 2003-10-06 22:55       ` Erwin Brandstetter <Brsaz@gmzx.at>
  0 siblings, 0 replies; 6+ messages in thread

From: Erwin Brandstetter @ 2003-10-06 22:55 UTC (permalink / raw)
  To: pgsql-admin

mail@joeconway.com (Joe Conway) wrote in 
news:3F80D8B1.7070407@joeconway.com:

> http://developer.postgresql.org/docs/postgres/functions-array.html

Very good!

PostgreSQL 7.4 will provide the functions "array_lower" and "array_upper" 
doing exactly what I was looking for.

Unfortunately I have to implement the functionality now. Can't wait till 
7.4 is availiable on our productive system. :(

Regards
Erwin

-- 
no z in my mail.



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2003-10-06 22:55 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2003-09-22 20:21 Return last value in of array (PostgreSQL 7.2.1) Erwin Brandstetter <Brsaz@gmzx.at>
2003-09-24 00:46 ` Steve Crawford <scrawford@pinpointresearch.com>
2003-09-29 16:54   ` Erwin Brandstetter <Brsaz@gmzx.at>
2003-09-29 19:03     ` Erwin Brandstetter <Brsaz@gmzx.at>
2003-10-06 02:51     ` Joe Conway <mail@joeconway.com>
2003-10-06 22:55       ` Erwin Brandstetter <Brsaz@gmzx.at>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox