Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 1B4FB632F3D for ; Wed, 2 Sep 2009 08:26:24 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 14105-10 for ; Wed, 2 Sep 2009 11:26:15 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from moutng.kundenserver.de (moutng.kundenserver.de [212.227.126.187]) by mail.postgresql.org (Postfix) with ESMTP id B2484632D69 for ; Wed, 2 Sep 2009 08:26:15 -0300 (ADT) Received: from webserv.wug.de ([194.25.154.138]) by mrelayeu.kundenserver.de (node=mreu0) with ESMTP (Nemesis) id 0MKuxg-1Minye2anS-000RPn; Wed, 02 Sep 2009 13:26:13 +0200 Received: from kretschmer by webserv.wug.de with local (Exim 3.36 #1) id 1Minyc-0003PI-00 for pgsql-sql@postgresql.org; Wed, 02 Sep 2009 13:26:10 +0200 Date: Wed, 2 Sep 2009 13:26:10 +0200 From: "A. Kretschmer" To: pgsql-sql@postgresql.org Subject: Re: Min and max element of an array column Message-ID: <20090902112610.GD31088@a-kretschmer.de> References: <4A9E4FC0.1010908@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <4A9E4FC0.1010908@gmail.com> X-OS: Debian/GNU Linux - weil ich es mir Wert bin! User-Agent: Mutt/1.5.9i X-Provags-ID: V01U2FsdGVkX19D8Jsip9PLvxDPdmwb7/sgZhnvnCgwSXoqXrB 9c/rkQ0pR7Y5wopyZps8YvdY78oKQv4UW7wwQ9ojF2G4+gRLns 2hi2QqvdpXZn0ofk+lSoQ== X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.599 tagged_above=-10 required=5 tests=BAYES_00=-2.599 X-Spam-Level: X-Archive-Number: 200909/13 X-Sequence-Number: 33078 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