Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 72CFA634DA0 for ; Fri, 25 Jun 2010 13:29:12 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 05063-03-5 for ; Fri, 25 Jun 2010 16:29:05 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 97B1D632DFE for ; Fri, 25 Jun 2010 13:29:02 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o5PGT0Yb005110; Fri, 25 Jun 2010 12:29:00 -0400 (EDT) To: Lee Hachadoorian cc: pgsql-sql Subject: Re: Average of Array? In-reply-to: References: Comments: In-reply-to Lee Hachadoorian message dated "Fri, 25 Jun 2010 12:21:01 -0400" Date: Fri, 25 Jun 2010 12:29:00 -0400 Message-ID: <5109.1277483340@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201006/88 X-Sequence-Number: 34279 Lee Hachadoorian writes: > Is there a function that returns the average of the elements of an > array? I'm thinking of something that would work like the avg() > aggregate function where it returns the average of all non-NULL > values. Can't find anything like it in the docs, and I'd like to make > sure I'm not missing something. You could unnest() the array and then apply the aggregate to the result. unnest() is only built in in 8.4 and later, but it's not terribly hard to write your own in previous versions. regards, tom lane