pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Min and max element of an array column
2+ messages / 2 participants
[nested] [flat]

* Min and max element of an array column
@ 2009-09-02 10:58 Gianvito Pio <pio.gianvito@gmail.com>
  2009-09-02 11:26 ` Re: Min and max element of an array column A. Kretschmer <andreas.kretschmer@schollglas.com>
  0 siblings, 1 reply; 2+ messages in thread

From: Gianvito Pio @ 2009-09-02 10:58 UTC (permalink / raw)
  To: pgsql-sql

Hello,
is it possible to obtain the minimum and the maximum single element of 
an array column?

Example:
[1, 2 ,5]
[3, -1, 6]
[9, 18,-4 ]

I'd just like to make a query that gives me the min (-4) and the max(18) 
of the column. Is that possible without a function? Thanks



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

* Re: Min and max element of an array column
  2009-09-02 10:58 Min and max element of an array column Gianvito Pio <pio.gianvito@gmail.com>
@ 2009-09-02 11:26 ` A. Kretschmer <andreas.kretschmer@schollglas.com>
  0 siblings, 0 replies; 2+ messages in thread

From: A. Kretschmer @ 2009-09-02 11:26 UTC (permalink / raw)
  To: pgsql-sql

In response to Gianvito Pio :
> Hello,
> is it possible to obtain the minimum and the maximum single element of 
> an array column?
> 
> Example:
> [1, 2 ,5]
> [3, -1, 6]
> [9, 18,-4 ]
> 
> I'd just like to make a query that gives me the min (-4) and the max(18) 
> of the column. Is that possible without a function? Thanks

test=*# select * from pio;
     i
-----------
 {1,2,5}
 {3,-1,6}
 {9,18,-4}
(3 rows)

test=*# select min(unnest), max(unnest) from (select unnest(i) from pio)
foo;
 min | max
-----+-----
  -4 |  18
(1 row)



It is 8.4, if you have a version < 8.4, you need the unnest-function:

CREATE OR REPLACE FUNCTION unnest(ANYARRAY) RETURNS SETOF ANYELEMENT
LANGUAGE SQL AS $$SELECT $1[i] FROM
generate_series(array_lower($1,1),array_upper($1,1)) i;$$;

(with Thx to David Fetter)


Andreas
-- 
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net



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


end of thread, other threads:[~2009-09-02 11:26 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-09-02 10:58 Min and max element of an array column Gianvito Pio <pio.gianvito@gmail.com>
2009-09-02 11:26 ` A. Kretschmer <andreas.kretschmer@schollglas.com>

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